Python asyncio.Event() Examples

The following are 30 code examples of asyncio.Event(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asyncio , or try the search function .
Example #1
Source File: help_channels.py    From bot with MIT License 7 votes vote down vote up
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()) 
Example #2
Source File: duofern_stick.py    From pyduofern with GNU General Public License v2.0 7 votes vote down vote up
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 
Example #3
Source File: test_watchers.py    From aiozk with MIT License 6 votes vote down vote up
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) 
Example #4
Source File: protocol.py    From pyquarkchain with MIT License 6 votes vote down vote up
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) 
Example #5
Source File: api.py    From bot with MIT License 6 votes vote down vote up
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() 
Example #6
Source File: band_lpv2.py    From huawei-lpv2 with MIT License 6 votes vote down vote up
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 
Example #7
Source File: core.py    From p2p-python with MIT License 6 votes vote down vote up
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) 
Example #8
Source File: core.py    From p2p-python with MIT License 6 votes vote down vote up
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 
Example #9
Source File: py3test_io_tcp.py    From moler with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 --------------------------- 
Example #10
Source File: discovery.py    From pyquarkchain with MIT License 6 votes vote down vote up
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 
Example #11
Source File: comms.py    From kivy-smoothie-host with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #12
Source File: test_duofern_stick_async.py    From pyduofern with GNU General Public License v2.0 6 votes vote down vote up
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() 
Example #13
Source File: performance.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
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() 
Example #14
Source File: task_queue.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
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 
Example #15
Source File: protocol.py    From aioh2 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #16
Source File: test_watchers.py    From aiozk with MIT License 6 votes vote down vote up
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) 
Example #17
Source File: test_watchers.py    From aiozk with MIT License 6 votes vote down vote up
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) 
Example #18
Source File: manager.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
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"] 
Example #19
Source File: request.py    From quart with MIT License 6 votes vote down vote up
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() 
Example #20
Source File: test_election.py    From aiozk with MIT License 6 votes vote down vote up
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) 
Example #21
Source File: initial_setup.py    From friendly-telegram with GNU Affero General Public License v3.0 6 votes vote down vote up
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 = {} 
Example #22
Source File: frontend.py    From friendly-telegram with GNU Affero General Public License v3.0 6 votes vote down vote up
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? 
Example #23
Source File: test_ws_transport.py    From aries-cloudagent-python with Apache License 2.0 5 votes vote down vote up
def test_message_and_response(self):
        await self.transport.start()

        test_message = {"test": "message"}
        test_response = {"response": "ok"}

        async with self.client.ws_connect("/") as ws:
            self.result_event = asyncio.Event()
            await ws.send_json(test_message)
            await asyncio.wait((self.result_event.wait(),), timeout=0.1)

            assert self.session is not None
            assert len(self.message_results) == 1
            received, receipt, can_respond = self.message_results[0]
            assert received == test_message
            assert can_respond

            response = OutboundMessage(
                payload=None, enc_payload=json.dumps(test_response)
            )
            self.session.set_response(response)

            result = await asyncio.wait_for(ws.receive_json(), 1.0)
            assert result == {"response": "ok"}

        await self.transport.stop() 
Example #24
Source File: token.py    From pyquarkchain with MIT License 5 votes vote down vote up
def __init__(self, name: str, loop: asyncio.AbstractEventLoop = None) -> None:
        self.name = name
        self._chain = []  # : List['CancelToken']
        self._triggered = asyncio.Event(loop=loop)
        self._loop = loop 
Example #25
Source File: websocket_client.py    From gabriel with Apache License 2.0 5 votes vote down vote up
def __init__(self, num_tokens):
        self._num_tokens = num_tokens
        self._event = asyncio.Event()
        self._frame_id = 0 
Example #26
Source File: service.py    From pyquarkchain with MIT License 5 votes vote down vote up
def __init__(self) -> None:
        self.started = asyncio.Event()
        self.stopped = asyncio.Event()
        self.cleaned_up = asyncio.Event()
        self.cancelled = asyncio.Event()
        self.finished = asyncio.Event() 
Example #27
Source File: discovery.py    From pyquarkchain with MIT License 5 votes vote down vote up
def wait_pong_v5(
        self, remote: kademlia.Node, token: Hash32
    ) -> Tuple[bool, bytes, List[float]]:
        event = asyncio.Event()
        wait_periods = []  # : List[float]
        pong = None  # : bytes

        def callback(raw_msg: bytes, wps: List[float]) -> None:
            nonlocal pong, wait_periods
            event.set()
            pong = raw_msg
            wait_periods = wps

        got_pong = await self._wait_pong(remote, token, event, callback)
        return got_pong, pong, wait_periods 
Example #28
Source File: discovery.py    From pyquarkchain with MIT License 5 votes vote down vote up
def _wait_pong(
        self,
        remote: kademlia.Node,
        token: Hash32,
        event: asyncio.Event,
        callback: Callable[..., Any],
    ) -> bool:
        """Wait for a pong from the given remote containing the given token.

        This coroutine adds a callback to pong_callbacks and yields control until the given event
        is set or a timeout (k_request_timeout) occurs. At that point it returns whether or not
        a pong was received with the given pingid.
        """
        pingid = self._mkpingid(token, remote)

        with self.pong_callbacks.acquire(pingid, callback):
            got_pong = False
            try:
                got_pong = await self.cancel_token.cancellable_wait(
                    event.wait(), timeout=kademlia.k_request_timeout
                )
                self.logger.trace("got expected pong with token %s", encode_hex(token))
            except TimeoutError:
                self.logger.trace(
                    "timed out waiting for pong from %s (token == %s)",
                    remote,
                    encode_hex(token),
                )

        return got_pong 
Example #29
Source File: discovery.py    From pyquarkchain with MIT License 5 votes vote down vote up
def wait_pong_v4(self, remote: kademlia.Node, token: Hash32) -> bool:
        event = asyncio.Event()
        callback = event.set
        return await self._wait_pong(remote, token, event, callback) 
Example #30
Source File: test_service.py    From pyquarkchain with MIT License 5 votes vote down vote up
def test_service_tasks_do_not_leak_memory():
    service = WaitService()
    asyncio.ensure_future(service.run())

    end = asyncio.Event()

    async def run_until_end():
        await end.wait()

    service.run_task(run_until_end())

    # inspect internals to determine if memory is leaking

    # confirm that task is tracked:
    assert len(service._tasks) == 1

    end.set()
    # allow the coro to exit
    await asyncio.sleep(0)

    # due to pypy, either use gc.collect or call service.gc()
    # gc.collect() # https://bitbucket.org/pypy/pypy/issues/1269/weakrefweakset-does-not-work-correctly
    service.gc()

    # confirm that task is no longer tracked:
    assert len(service._tasks) == 0

    # test cleanup
    await service.cancel()