Python源码示例:asyncio.Event()

示例1
def __init__(self, bot: Bot):
        super().__init__()

        self.bot = bot

        # Categories
        self.available_category: discord.CategoryChannel = None
        self.in_use_category: discord.CategoryChannel = None
        self.dormant_category: discord.CategoryChannel = None

        # Queues
        self.channel_queue: asyncio.Queue[discord.TextChannel] = None
        self.name_queue: t.Deque[str] = None

        self.name_positions = self.get_names()
        self.last_notification: t.Optional[datetime] = None

        # Asyncio stuff
        self.queue_tasks: t.List[asyncio.Task] = []
        self.ready = asyncio.Event()
        self.on_message_lock = asyncio.Lock()
        self.init_task = self.bot.loop.create_task(self.init_cog()) 
示例2
def __init__(self, loop=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.duofern_parser.asyncio = True
        self.initialization_step = 0
        self.loop = loop
        self.write_queue = asyncio.Queue()
        self._ready = asyncio.Event()
        self.transport = None
        self.buffer = bytearray(b'')

        self.last_packet = 0.0
        self.callback = None

        if loop == None:
            loop = asyncio.get_event_loop()

        self.send_loop = asyncio.ensure_future(self._send_messages(), loop=loop)

        self.available = asyncio.Future()

        # DuofernStick.__init__(self, device, system_code, config_file_json, duofern_parser)

    #        self.serial_connection = serial.Serial(self.port, baudrate=115200, timeout=1)
    #        self.running = False 
示例3
def __init__(self, **kwargs):
        self.heroku_api_token = os.environ.get("heroku_api_token")
        self.api_token = kwargs.pop("api_token")
        self.redirect_url = None
        super().__init__(**kwargs)
        self.app.router.add_get("/initialSetup", self.initial_setup)
        self.app.router.add_put("/setApi", self.set_tg_api)
        self.app.router.add_post("/sendTgCode", self.send_tg_code)
        self.app.router.add_post("/tgCode", self.tg_code)
        self.app.router.add_post("/finishLogin", self.finish_login)
        self.api_set = asyncio.Event()
        self.sign_in_clients = {}
        self.clients = []
        self.clients_set = asyncio.Event()
        self.root_redirected = asyncio.Event()
        self._pending_secret_to_uid = {} 
示例4
def __init__(self, backend, noop=False):
        super().__init__()
        self._noop = noop or backend is None
        self._backend = backend
        self._pending = None
        self._loading = True
        self._waiter = asyncio.Event()
        self._sync_future = None
        # We use a future because we need await-ability and we will be delaying by 10s, but
        # because we are gonna frequently be changing the data, we want to avoid floodwait
        # and to do that we will discard most requests. However, attempting to await any request
        # should return a future corresponding to the next time that we flush the database.
        # To achieve this, we have one future stored here (the next time we flush the db) and we
        # always return that from set(). However, if someone decides to await set() much later
        # than when they called set(), it will already be finished. Luckily, we return a future,
        # not a reference to _sync_future, so it will be the correct future, and set_result will
        # not already have been called. Simple, right? 
示例5
def __init__(
        self, expected_content_length: Optional[int], max_content_length: Optional[int]
    ) -> None:
        self._data = bytearray()
        self._complete: asyncio.Event = asyncio.Event()
        self._has_data: asyncio.Event = asyncio.Event()
        self._max_content_length = max_content_length
        # Exceptions must be raised within application (not ASGI)
        # calls, this is achieved by having the ASGI methods set this
        # to an exception on error.
        self._must_raise: Optional[Exception] = None
        if (
            expected_content_length is not None
            and max_content_length is not None
            and expected_content_length > max_content_length
        ):
            from ..exceptions import RequestEntityTooLarge  # noqa Avoiding circular import

            self._must_raise = RequestEntityTooLarge() 
示例6
def __init__(self, app, reportrate=1):
        self.app = app
        self.proto = None
        self.timer = None
        self._fragment = None
        self.abort_stream = False
        self.pause_stream = False  # asyncio.Event()
        self.okcnt = None
        self.ping_pong = True  # ping pong protocol for streaming
        self.file_streamer = None
        self.report_rate = reportrate
        self._reroute_incoming_data_to = None
        self._restart_timer = False
        self.is_streaming = False
        self.do_query = False
        self.last_tool = None
        self.is_suspend = False
        self.m0 = None
        self.net_connection = False
        self.log = logging.getLogger()  # .getChild('Comms')
        # logging.getLogger().setLevel(logging.DEBUG) 
示例7
def __init__(self, loop: asyncio.AbstractEventLoop, **kwargs):
        auth_headers = {
            'Authorization': f"Token {Keys.site_api}"
        }

        if 'headers' in kwargs:
            kwargs['headers'].update(auth_headers)
        else:
            kwargs['headers'] = auth_headers

        self.session = None
        self.loop = loop

        self._ready = asyncio.Event(loop=loop)
        self._creation_task = None
        self._default_session_kwargs = kwargs

        self.recreate() 
示例8
def test_can_receive_binary_data_from_connection(tcp_connection_class,
                                                       integration_tcp_server_and_pipe):
    from moler.threaded_moler_connection import ThreadedMolerConnection
    (tcp_server, tcp_server_pipe) = integration_tcp_server_and_pipe
    received_data = bytearray()
    receiver_called = asyncio.Event()

    def receiver(data, time_recv):
        received_data.extend(data)
        receiver_called.set()

    moler_conn = ThreadedMolerConnection()  # no decoder, just pass bytes 1:1
    moler_conn.subscribe(receiver)       # build forwarding path
    connection = tcp_connection_class(moler_connection=moler_conn, port=tcp_server.port, host=tcp_server.host)
    async with connection:  # TODO: async with connection.open():
        time.sleep(0.1)  # otherwise we have race between server's pipe and from-client-connection
        tcp_server_pipe.send(("send async msg", {'msg': b'data to read'}))
        await asyncio.wait_for(receiver_called.wait(), timeout=0.5)

    assert b'data to read' == received_data


# TODO: tests for error cases raising Exceptions

# --------------------------- resources --------------------------- 
示例9
def test_election_early_wait_for_leadership(zk, path):
    elec = zk.recipes.LeaderElection(path)

    early_wait_success = asyncio.Event()

    async def wait_early():
        await elec.wait_for_leadership()
        assert elec.has_leadership
        early_wait_success.set()

    asyncio.create_task(wait_early())
    await asyncio.sleep(0.5)
    assert not elec.has_leadership

    await elec.volunteer()

    # NO WAIT
    await asyncio.wait_for(early_wait_success.wait(), timeout=0.5)

    await elec.resign()

    assert not elec.has_leadership

    await zk.delete(path) 
示例10
def test_data_watch(zk, path, data_watcher):
    data = []
    ready = asyncio.Event()
    test_data = b'test' * 1000

    async def data_callback(d):
        data.append(d)
        ready.set()

    data_watcher.add_callback(path, data_callback)
    assert data == []
    await zk.set_data(path, test_data)
    await asyncio.wait_for(ready.wait(), timeout=0.1)
    assert ready.is_set()
    assert data == [test_data]
    data_watcher.remove_callback(path, data_callback) 
示例11
def test_data_watch_delete(zk, path, data_watcher):
    data = []
    ready = asyncio.Event()
    test_data = b'test'

    async def data_callback(d):
        data.append(d)
        ready.set()

    await zk.set_data(path, test_data)

    data_watcher.add_callback(path, data_callback)
    await asyncio.sleep(0.2)
    assert data == [test_data]
    ready.clear()
    await zk.delete(path)

    await asyncio.wait_for(ready.wait(), timeout=1)
    assert ready.is_set()
    assert data == [test_data, NoNode]
    data_watcher.remove_callback(path, data_callback)

    await zk.create(path) 
示例12
def test_child_watch(child_watcher, path, zk, child1, child2):
    children = set()
    ready = asyncio.Event()

    async def children_callback(c):
        for child in c:
            children.add(child)
            ready.set()

    child_watcher.add_callback(path, children_callback)
    assert children == set()
    await zk.create(child1)
    await asyncio.wait([ready.wait()], timeout=0.1)
    assert children == {child1.split('/')[-1]}
    ready.clear()
    await zk.create(child2)
    await asyncio.wait([ready.wait()], timeout=0.1)
    assert ready.is_set()
    assert children == {child.split('/')[-1] for child in (child1, child2)}
    child_watcher.remove_callback(path, children_callback) 
示例13
def __init__(self, stream_id, window_getter, loop=None):
        if loop is None:
            loop = asyncio.get_event_loop()
        self._stream_id = stream_id
        self._window_getter = window_getter

        self._wlock = asyncio.Lock(loop=loop)
        self._window_open = CallableEvent(self._is_window_open, loop=loop)

        self._rlock = asyncio.Lock(loop=loop)
        self._buffers = deque()
        self._buffer_size = 0
        self._buffer_ready = asyncio.Event(loop=loop)
        self._response = asyncio.Future(loop=loop)
        self._trailers = asyncio.Future(loop=loop)
        self._eof_received = False
        self._closed = False 
示例14
def wait_ping(self, remote: kademlia.Node) -> bool:
        """Wait for a ping from the given remote.

        This coroutine adds a callback to ping_callbacks and yields control until that callback is
        called or a timeout (k_request_timeout) occurs. At that point it returns whether or not
        a ping was received from the given node.
        """
        event = asyncio.Event()

        with self.ping_callbacks.acquire(remote, event.set):
            got_ping = False
            try:
                got_ping = await self.cancel_token.cancellable_wait(
                    event.wait(), timeout=kademlia.k_request_timeout
                )
                self.logger.trace("got expected ping from %s", remote)
            except TimeoutError:
                self.logger.trace("timed out waiting for ping from %s", remote)

        return got_ping 
示例15
def __init__(
        self,
        proxy_conn,
        op_ser_map,
        op_non_rpc_map,
        op_rpc_map,
        loop=None,
        metadata_class=Metadata,
        name=None,
    ):
        super().__init__(
            op_ser_map, op_non_rpc_map, op_rpc_map, loop, metadata_class, name=name
        )
        self.read_deque = deque()
        self.read_event = asyncio.Event()
        self.proxy_conn = proxy_conn
        self.forward_conn = ForwardingVirtualConnection(self) 
示例16
def __init__(self, loop, client: BleakClient, client_mac: str, device_mac: str, key: bytes):
        self.state: BandState = BandState.Disconnected

        self.client: BleakClient = client
        self.loop = loop

        self.client_mac: str = client_mac
        self.device_mac: str = device_mac
        self.client_serial: str = client_mac.replace(":", "")[-6:]  # android.os.Build.SERIAL

        self._key: bytes = key
        self._server_nonce: Optional[bytes] = None
        self._client_nonce: bytes = generate_nonce()
        self._encryption_counter: int = 0

        self.link_params: Optional[device_config.LinkParams] = None

        self.bond_status: Optional[int] = None
        self.bond_status_info: Optional[int] = None
        self.bt_version: Optional[int] = None

        self._packet: Optional[Packet] = None
        self._event = asyncio.Event()
        self.__message_id: int = -1 
示例17
def __init__(self, host=None, listen=15):
        assert V.DATA_PATH is not None, 'Setup p2p params before CoreClass init.'
        assert host is None or host == 'localhost'
        # status params
        self.f_stop = False
        self.f_finish = False
        self.f_running = False
        # working info
        self.start_time = int(time())
        self.number = 0
        self.user: List[User] = list()
        self.user_lock = asyncio.Lock()
        self.host = host  # local=>'localhost', 'global'=>None
        self.core_que = asyncio.Queue()
        self.backlog = listen
        self.traffic = Traffic()
        self.ping_status: Dict[int, asyncio.Event] = ExpiringDict(max_len=5000, max_age_seconds=900) 
示例18
def ping(self, user: User, f_udp=False):
        uuid = random.randint(1000000000, 4294967295)
        try:
            # prepare Event
            event = asyncio.Event()
            self.ping_status[uuid] = event
            # send ping
            msg_body = b'Ping:' + str(uuid).encode()
            await self.send_msg_body(msg_body=msg_body, user=user, allow_udp=f_udp, f_pro_force=True)
            # wait for event set (5s)
            await asyncio.wait_for(event.wait(), 5.0)
            return True
        except asyncio.TimeoutError:
            log.debug(f"failed to udp ping {user}")
        except ConnectionError as e:
            log.debug(f"socket error on ping by {e}")
        except Exception:
            log.error("ping exception", exc_info=True)
        # failed
        return False 
示例19
def test_init_against_mocked_stick(looproto):
    loop, proto = looproto
    proto.transport = TransportMock(proto)
    proto._ready = asyncio.Event()

    initialization = asyncio.ensure_future(proto.handshake())

    proto._ready.set()

    def cb(a):
        logging.info(a)

    proto.available.add_done_callback(cb)

    loop.run_until_complete(initialization)

    for task in asyncio.Task.all_tasks():
        task.cancel() 
示例20
def __init__(
        self,
        ident: str,
        port: int,
        prefix: str = None,
        use_routing: bool = False,
        **kwargs,
    ):
        if prefix is None:
            prefix = ident
        super().__init__(ident, port, port + 1, prefix=prefix, **kwargs)
        self._connection_id = None
        self._connection_ready = None
        self.credential_state = {}
        self.credential_event = asyncio.Event()
        self.revocations = []
        self.ping_state = {}
        self.ping_event = asyncio.Event()
        self.sent_pings = set() 
示例21
def __init__(
        self, max_active: int = 0, timed: bool = False, trace_fn: Callable = None
    ):
        """
        Initialize the task queue.

        Args:
            max_active: The maximum number of tasks to automatically run
            timed: A flag indicating that timing should be collected for tasks
            trace_fn: A callback for all completed tasks
        """
        self.loop = asyncio.get_event_loop()
        self.active_tasks = []
        self.pending_tasks = []
        self.timed = timed
        self.total_done = 0
        self.total_failed = 0
        self.total_started = 0
        self._trace_fn = trace_fn
        self._cancelled = False
        self._drain_evt = asyncio.Event()
        self._drain_task: asyncio.Task = None
        self._max_active = max_active 
示例22
def __init__(
        self, context: InjectionContext, handle_not_delivered: Callable = None
    ):
        """
        Initialize a `OutboundTransportManager` instance.

        Args:
            context: The application context
            handle_not_delivered: An optional handler for undelivered messages

        """
        self.context = context
        self.loop = asyncio.get_event_loop()
        self.handle_not_delivered = handle_not_delivered
        self.outbound_buffer = []
        self.outbound_event = asyncio.Event()
        self.outbound_new = []
        self.registered_schemes = {}
        self.registered_transports = {}
        self.running_transports = {}
        self.task_queue = TaskQueue(max_active=200)
        self._process_task: asyncio.Task = None
        if self.context.settings.get("transport.max_outbound_retry"):
            self.MAX_RETRY_COUNT = self.context.settings["transport.max_outbound_retry"] 
示例23
def main(loop):
    # Create a shared event
    event = asyncio.Event()
    print('event start state: {}'.format(event.is_set()))

    loop.call_later(
        0.1, functools.partial(set_event, event)
    )

    await asyncio.wait([coro1(event), coro2(event)])
    print('event end state: {}'.format(event.is_set())) 
示例24
def __init__(self, **kwargs):
        self.runner = None
        self.port = None
        self.running = asyncio.Event()
        self.ready = asyncio.Event()
        self.client_data = {}
        self._ratelimit_data = collections.defaultdict(dict)
        self.app = web.Application(middlewares=[ratelimit(lambda f: self._ratelimit_data[f])])
        aiohttp_jinja2.setup(self.app, filters={"getdoc": inspect.getdoc, "ascii": ascii},
                             loader=jinja2.FileSystemLoader("web-resources"))
        self.app["static_root_url"] = "/static"
        super().__init__(**kwargs)
        self.app.router.add_static("/static/", "web-resources/static") 
示例25
def test_websocket_bp_route_name(app):
    """Tests that blueprint websocket route is named."""
    event = asyncio.Event()
    bp = Blueprint("test_bp", url_prefix="/bp")

    @bp.get("/main")
    async def main(request):
        ...

    @bp.websocket("/route")
    async def test_route(request, ws):
        event.set()

    @bp.websocket("/route2")
    async def test_route2(request, ws):
        event.set()

    @bp.websocket("/route3", name="foobar_3")
    async def test_route3(request, ws):
        event.set()

    app.blueprint(bp)

    uri = app.url_for("test_bp.main")
    assert uri == "/bp/main"

    uri = app.url_for("test_bp.test_route")
    assert uri == "/bp/route"
    request, response = app.test_client.websocket(uri)
    assert response.opened is True
    assert event.is_set()

    event.clear()
    uri = app.url_for("test_bp.test_route2")
    assert uri == "/bp/route2"
    request, response = app.test_client.websocket(uri)
    assert response.opened is True
    assert event.is_set()

    uri = app.url_for("test_bp.foobar_3")
    assert uri == "/bp/route3" 
示例26
def test_websocket_route(app, url):
    ev = asyncio.Event()

    @app.websocket(url)
    async def handler(request, ws):
        assert request.scheme == "ws"
        assert ws.subprotocol is None
        ev.set()

    request, response = app.test_client.websocket(url)
    assert response.opened is True
    assert ev.is_set() 
示例27
def test_websocket_route_asgi(app, url):
    ev = asyncio.Event()

    @app.websocket(url)
    async def handler(request, ws):
        ev.set()

    request, response = await app.asgi_client.websocket(url)
    assert ev.is_set() 
示例28
def test_add_webscoket_route(app, strict_slashes):
    ev = asyncio.Event()

    async def handler(request, ws):
        assert ws.subprotocol is None
        ev.set()

    app.add_websocket_route(handler, "/ws", strict_slashes=strict_slashes)
    request, response = app.test_client.websocket("/ws")
    assert response.opened is True
    assert ev.is_set() 
示例29
def test_add_webscoket_route_with_version(app):
    ev = asyncio.Event()

    async def handler(request, ws):
        assert ws.subprotocol is None
        ev.set()

    app.add_websocket_route(handler, "/ws", version=1)
    request, response = app.test_client.websocket("/v1/ws")
    assert response.opened is True
    assert ev.is_set() 
示例30
def test_websocket_route(app: Sanic):
    event = asyncio.Event()

    async def websocket_handler(request, ws):
        assert ws.subprotocol is None
        event.set()

    bp = Blueprint(name="handler", url_prefix="/ws")
    bp.add_websocket_route(websocket_handler, "/test", name="test")

    app.blueprint(bp)

    _, response = app.test_client.websocket("/ws/test")
    assert response.opened is True
    assert event.is_set()