Python trio.Event() Examples

The following are 30 code examples of trio.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 trio , or try the search function .
Example #1
Source File: test_trio_endpoint_wait_for.py    From lahja with MIT License 6 votes vote down vote up
def test_trio_endpoint_wait_for(endpoint_pair):
    alice, bob = endpoint_pair

    # NOTE: this test is the inverse of the broadcast test
    event = EventTest("test")

    done = trio.Event()

    async def _do_wait_for():
        result = await alice.wait_for(EventTest)

        assert isinstance(result, EventTest)
        assert result.value == "test"
        done.set()

    async with trio.open_nursery() as nursery:
        nursery.start_soon(_do_wait_for)

        await bob.wait_until_endpoint_subscribed_to(alice.name, EventTest)

        await bob.broadcast(event)
        await done.wait() 
Example #2
Source File: test_logged_core.py    From parsec-cloud with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_init_online_backend_late_reply(
    server_factory, core_config, alice, event_bus, backend
):
    can_serve_client = trio.Event()

    async def _handle_client(stream):
        await can_serve_client.wait()
        return await backend.handle_client(stream)

    async with server_factory(_handle_client, alice.organization_addr):
        with trio.fail_after(1):
            async with logged_core_factory(
                config=core_config, device=alice, event_bus=event_bus
            ) as core:
                # We don't want for backend to reply finish core init
                with core.event_bus.listen() as spy:
                    can_serve_client.set()
                    # Now backend reply, monitor should send events accordingly
                    await spy.wait(
                        CoreEvent.BACKEND_CONNECTION_CHANGED,
                        kwargs={"status": BackendConnStatus.READY, "status_exc": None},
                    ) 
Example #3
Source File: test_logged_core.py    From parsec-cloud with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_init_offline_backend_late_reply(server_factory, core_config, alice, event_bus):
    can_serve_client = trio.Event()

    async def _handle_client(stream):
        await can_serve_client.wait()
        await stream.aclose()

    async with server_factory(_handle_client, alice.organization_addr):
        with trio.fail_after(1):
            async with logged_core_factory(
                config=core_config, device=alice, event_bus=event_bus
            ) as core:
                with core.event_bus.listen() as spy:
                    can_serve_client.set()
                    await spy.wait(
                        CoreEvent.BACKEND_CONNECTION_CHANGED,
                        kwargs={"status": BackendConnStatus.LOST, "status_exc": ANY},
                    ) 
Example #4
Source File: schedule.py    From starbelly with MIT License 6 votes vote down vote up
def __init__(self, db, crawl_manager):
        '''
        Constructor.

        :param starbelly.db.ScheduleDB db:
        :param starbelly.job.CrawlManager crawl_manager: A crawl manager, used
            for starting jobs when they are scheduled to begin.
        '''
        self._db = db
        self._crawl_manager = crawl_manager

        self._events = list()
        self._schedules = dict()
        self._recv_channel = crawl_manager.get_job_state_channel()
        self._event_added = trio.Event()
        self._nursery = None
        self._running_jobs = dict() 
Example #5
Source File: endpoint.py    From lahja with MIT License 6 votes vote down vote up
def __init__(
        self,
        local_name: str,
        conn: ConnectionAPI,
        subscriptions_changed: ConditionAPI,
        new_msg_func: Callable[[Broadcast], Awaitable[Any]],
    ) -> None:
        super().__init__(local_name, conn, new_msg_func)

        self._notify_lock = trio.Lock()  # type: ignore

        self._received_response = trio.Condition()  # type: ignore
        self._received_subscription = trio.Condition()  # type: ignore

        self._running = trio.Event()  # type: ignore
        self._stopped = trio.Event()  # type: ignore

        self._received_subscription = subscriptions_changed

        self._subscriptions_initialized = trio.Event()  # type: ignore

        self._running = trio.Event()  # type: ignore
        self._stopped = trio.Event()  # type: ignore
        self._ready = trio.Event()  # type: ignore 
Example #6
Source File: endpoint.py    From lahja with MIT License 6 votes vote down vote up
def _monitor_subscription_changes(self) -> None:
        while not self.is_stopped:
            # We wait for the event to change and then immediately replace it
            # with a new event.  This **must** occur before any additional
            # `await` calls to ensure that any *new* changes to the
            # subscriptions end up operating on the *new* event and will be
            # picked up in the next iteration of the loop.
            await self._subscriptions_changed.wait()
            self._subscriptions_changed = trio.Event()

            # make a copy so that the set doesn't change while we iterate
            # over it
            subscribed_events = self.get_subscribed_events()

            async with trio.open_nursery() as nursery:
                async with self._remote_connections_changed:
                    for remote in self._connections:
                        nursery.start_soon(
                            remote.notify_subscriptions_updated,
                            subscribed_events,
                            False,
                        )
            async with self._remote_subscriptions_changed:
                self._remote_subscriptions_changed.notify_all() 
Example #7
Source File: trio_thread.py    From parsec-cloud with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, trio_token, fn, args, kwargs, qt_on_success, qt_on_error):
        self._trio_token = trio_token
        assert isinstance(qt_on_success, ThreadSafeQtSignal)
        assert qt_on_success.args_types in ((), (QtToTrioJob,))
        self._qt_on_success = qt_on_success
        assert isinstance(qt_on_error, ThreadSafeQtSignal)
        assert qt_on_error.args_types in ((), (QtToTrioJob,))
        self._qt_on_error = qt_on_error
        self._fn = fn
        self._args = args
        self._kwargs = kwargs
        self.cancel_scope = None
        self._started = trio.Event()
        self._done = threading.Event()
        self.status = None
        self.ret = None
        self.exc = None 
Example #8
Source File: messages_monitor.py    From parsec-cloud with GNU Affero General Public License v3.0 6 votes vote down vote up
def monitor_messages(user_fs, event_bus, task_status):
    wakeup = trio.Event()

    def _on_message_received(event, index):
        nonlocal wakeup
        wakeup.set()
        # Don't wait for the *actual* awakening to change the status to
        # avoid having a period of time when the awakening is scheduled but
        # not yet notified to task_status
        task_status.awake()

    with event_bus.connect_in_context((CoreEvent.BACKEND_MESSAGE_RECEIVED, _on_message_received)):
        try:
            await user_fs.process_last_messages()
            task_status.started()
            while True:
                task_status.idle()
                await wakeup.wait()
                wakeup = trio.Event()
                await freeze_messages_monitor_mockpoint()
                await user_fs.process_last_messages()

        except FSBackendOfflineError as exc:
            raise BackendNotAvailable from exc 
Example #9
Source File: test_connection.py    From trio-websocket with MIT License 6 votes vote down vote up
def test_no_messages_after_local_close(nursery):
    '''
    If the local endpoint initiates closing, then pending messages are discarded
    and any attempt to read a message will raise ConnectionClosed.
    '''
    client_closed = trio.Event()

    async def handler(request):
        # The server sends some messages and then closes.
        server = await request.accept()
        async with server:
            await server.send_message('1')
            await server.send_message('2')
            await client_closed.wait()

    server = await nursery.start(
        partial(serve_websocket, handler, HOST, 0, ssl_context=None))

    async with open_websocket(HOST, server.port, '/', use_ssl=False) as client:
        pass
    with pytest.raises(ConnectionClosed):
        await client.get_message()
    client_closed.set() 
Example #10
Source File: packer.py    From trinity with MIT License 6 votes vote down vote up
def start_handshake_as_recipient(self,
                                     auth_tag: Nonce,
                                     local_enr: ENR,
                                     remote_enr: Optional[ENR],
                                     ) -> None:
        if not self.is_pre_handshake:
            raise ValueError("Can only register handshake when its not started yet")

        self.handshake_participant = HandshakeRecipient(
            local_private_key=self.local_private_key,
            local_enr=local_enr,
            remote_node_id=self.remote_node_id,
            remote_enr=remote_enr,
            initiating_packet_auth_tag=auth_tag,
        )
        self.handshake_successful_event = trio.Event()
        self.manager.run_task(self.check_handshake_timeout, self.handshake_successful_event)

        if not self.is_during_handshake:
            raise Exception("Invariant: After a handshake is started, the handshake is in progress") 
Example #11
Source File: packer.py    From trinity with MIT License 6 votes vote down vote up
def start_handshake_as_initiator(self,
                                     local_enr: ENR,
                                     remote_enr: ENR,
                                     message: BaseMessage,
                                     ) -> None:
        if not self.is_pre_handshake:
            raise ValueError("Can only register handshake when its not started yet")

        self.handshake_participant = HandshakeInitiator(
            local_private_key=self.local_private_key,
            local_enr=local_enr,
            remote_enr=remote_enr,
            initial_message=message,
        )
        self.handshake_successful_event = trio.Event()
        self.manager.run_task(self.check_handshake_timeout, self.handshake_successful_event)

        if not self.is_during_handshake:
            raise Exception("Invariant: After a handshake is started, the handshake is in progress") 
Example #12
Source File: _impl.py    From trio-websocket with MIT License 6 votes vote down vote up
def _send(self, event):
        '''
        Send an to the remote WebSocket.

        The reader task and one or more writers might try to send messages at
        the same time, so this method uses an internal lock to serialize
        requests to send data.

        :param wsproto.events.Event event:
        '''
        data = self._wsproto.send(event)
        async with self._stream_lock:
            logger.debug('%s sending %d bytes', self, len(data))
            try:
                await self._stream.send_all(data)
            except (trio.BrokenResourceError, trio.ClosedResourceError):
                await self._abort_web_socket()
                raise ConnectionClosed(self._close_reason) from None 
Example #13
Source File: test_discovery.py    From trinity with MIT License 6 votes vote down vote up
def test_ping_pong(manually_driven_discovery_pair):
    alice, bob = manually_driven_discovery_pair
    # Collect all pongs received by alice in a list for later inspection.
    got_pong = trio.Event()
    received_pongs = []

    async def recv_pong(node, payload, hash_):
        received_pongs.append((node, payload))
        got_pong.set()

    alice.recv_pong_v4 = recv_pong

    token = await alice.send_ping_v4(bob.this_node)

    with trio.fail_after(1):
        await bob.consume_datagram()
        await alice.consume_datagram()
        await got_pong.wait()

    assert len(received_pongs) == 1
    node, payload = received_pongs[0]
    assert node.id == bob.this_node.id
    assert token == payload[1] 
Example #14
Source File: context.py    From hypercorn with MIT License 5 votes vote down vote up
def __init__(self) -> None:
        self._event = trio.Event() 
Example #15
Source File: _impl.py    From trio-websocket with MIT License 5 votes vote down vote up
def __init__(self):
        ''' Constructor. '''
        self._value = None
        self._value_event = trio.Event() 
Example #16
Source File: job.py    From starbelly with MIT License 5 votes vote down vote up
def __init__(self, name, job_id, schedule_id, policy, frontier, downloader,
            storage, extractor, terminator):
        '''
        Constructor.

        :param str name: The name of this job.
        :param str job_id: ID for this job, or None if its a new job.
        :param schedule_id: (Optional) The ID of the schedule associated with
            this job.
        :type schedule_id: str or None
        :param starbelly.policy.Policy: A crawl policy.
        :param starbelly.frontier.CrawlFrontier frontier: The component that
            manages the crawl priority queue.
        :param starbelly.downloader.Downloader downloader: The component that
            downloads resources.
        :param starbelly.storage.CrawlStorage storage: The component that stores
            crawl results in the database.
        :param starbelly.extractor.CrawlExtractor extractor: The component that
            extracts URLs from downloaded resources and adds them to the crawl
            frontier.
        :param PipelineTerminator terminator: The component that reads data off
            of the end of the crawling pipeline and discards it.
        '''
        self._name = name
        self._job_id = job_id
        self._schedule_id = schedule_id
        self._policy = policy
        self._frontier = frontier
        self._downloader = downloader
        self._storage = storage
        self._extractor = extractor
        self._terminator = terminator

        self._cancel_scope = None
        self._stopped = trio.Event() 
Example #17
Source File: schedule.py    From starbelly with MIT License 5 votes vote down vote up
def _add_event(self, event):
        '''
        Add an event to the internal queue.

        :param ScheduleEvent event:
        '''
        heapq.heappush(self._events, event)
        self._event_added.set()
        self._event_added = trio.Event() 
Example #18
Source File: future.py    From lahja with MIT License 5 votes vote down vote up
def __init__(self) -> None:
        self._cancelled = False
        self._did_error = False
        self._done = trio.Event() 
Example #19
Source File: packer.py    From trinity with MIT License 5 votes vote down vote up
def __init__(self,
                 local_private_key: bytes,
                 local_node_id: NodeID,
                 remote_node_id: NodeID,
                 node_db: NodeDBAPI,
                 message_type_registry: MessageTypeRegistry,
                 incoming_packet_receive_channel: ReceiveChannel[IncomingPacket],
                 incoming_message_send_channel: SendChannel[IncomingMessage],
                 outgoing_message_receive_channel: ReceiveChannel[OutgoingMessage],
                 outgoing_packet_send_channel: SendChannel[OutgoingPacket],
                 ) -> None:
        self.local_private_key = local_private_key
        self.local_node_id = local_node_id
        self.remote_node_id = remote_node_id
        self.node_db = node_db
        self.message_type_registry = message_type_registry

        self.incoming_packet_receive_channel = incoming_packet_receive_channel
        self.incoming_message_send_channel = incoming_message_send_channel
        self.outgoing_message_receive_channel = outgoing_message_receive_channel
        self.outgoing_packet_send_channel = outgoing_packet_send_channel

        self.logger = logging.getLogger(
            f"p2p.discv5.packer.PeerPacker[{encode_hex(remote_node_id)[2:10]}]"
        )

        self.outgoing_message_backlog: List[OutgoingMessage] = []

        # This lock ensures that at all times, at most one incoming packet or outgoing message is
        # being processed.
        self.handling_lock = trio.Lock()

        self.handshake_successful_event: Optional[trio.Event] = None 
Example #20
Source File: endpoint.py    From lahja with MIT License 5 votes vote down vote up
def __init__(self, name: str):
        super().__init__(name)

        self._running = trio.Event()
        self._stopped = trio.Event()

        # Temporary storage for SendChannels used in the `stream` API.  Uses a
        # `WeakSet` to automate cleanup.
        self._stream_channels = collections.defaultdict(set)
        self._pending_requests = {}
        self._sync_handlers = collections.defaultdict(list)
        self._async_handlers = collections.defaultdict(list)

        self._run_lock = trio.Lock()

        # Signal when a new remote connection is established
        self._remote_connections_changed = trio.Condition()  # type: ignore

        # Signal when at least one remote has had a subscription change.
        self._remote_subscriptions_changed = trio.Condition()  # type: ignore

        # events used to signal that the endpoint has fully booted up.
        self._message_processing_loop_running = trio.Event()
        self._connection_loop_running = trio.Event()
        self._process_broadcasts_running = trio.Event()

        # internal signal that local subscriptions have changed.
        self._subscriptions_changed = trio.Event()

        self._socket_bound = trio.Event()
        self._server_stopped = trio.Event()

    #
    # Running and endpoint
    # 
Example #21
Source File: endpoint.py    From lahja with MIT License 5 votes vote down vote up
def broadcast(
        self, item: BaseEvent, config: Optional[BroadcastConfig] = None
    ) -> None:
        """
        Broadcast an instance of :class:`~lahja.common.BaseEvent` on the event bus. Takes
        an optional second parameter of :class:`~lahja.common.BroadcastConfig` to decide
        where this event should be broadcasted to. By default, events are broadcasted across
        all connected endpoints with their consuming call sites.
        """
        self.maybe_raise_no_subscribers_exception(config, type(item))
        done = trio.Event()
        await self._outbound_send_channel.send((done, item, config, None))
        await done.wait() 
Example #22
Source File: test_trio_endpoint_stream.py    From lahja with MIT License 5 votes vote down vote up
def test_trio_endpoint_stream_with_limit(endpoint_pair):
    alice, bob = endpoint_pair

    async with trio.open_nursery() as nursery:
        done = trio.Event()

        async def _do_stream():
            results = []
            async for event in alice.stream(EventTest, num_events=4):
                results.append(event)
                assert len(results) <= 4

            assert len(results) == 4
            assert all(isinstance(result, EventTest) for result in results)

            values = [result.value for result in results]
            assert values == [0, 1, 2, 3]
            done.set()

        nursery.start_soon(_do_stream)

        await bob.wait_until_endpoint_subscribed_to(alice.name, EventTest)
        await bob.broadcast(EventTest(0))
        await bob.broadcast(EventTest(1))
        await bob.broadcast(EventTest(2))
        await bob.broadcast(EventTest(3))

        await done.wait() 
Example #23
Source File: _impl.py    From trio-websocket with MIT License 5 votes vote down vote up
def ping(self, payload=None):
        '''
        Send WebSocket ping to remote endpoint and wait for a correspoding pong.

        Each in-flight ping must include a unique payload. This function sends
        the ping and then waits for a corresponding pong from the remote
        endpoint.

        *Note: If the remote endpoint recieves multiple pings, it is allowed to
        send a single pong. Therefore, the order of calls to ``ping()`` is
        tracked, and a pong will wake up its corresponding ping as well as all
        previous in-flight pings.*

        :param payload: The payload to send. If ``None`` then a random 32-bit
            payload is created.
        :type payload: bytes or None
        :raises ConnectionClosed: if connection is closed.
        :raises ValueError: if ``payload`` is identical to another in-flight
            ping.
        '''
        if self._close_reason:
            raise ConnectionClosed(self._close_reason)
        if payload in self._pings:
            raise ValueError('Payload value {} is already in flight.'.
                format(payload))
        if payload is None:
            payload = struct.pack('!I', random.getrandbits(32))
        event = trio.Event()
        self._pings[payload] = event
        await self._send(Ping(payload=payload))
        await event.wait() 
Example #24
Source File: packer.py    From trinity with MIT License 5 votes vote down vote up
def check_handshake_timeout(self, handshake_successful_event: trio.Event) -> None:
        try:
            with trio.fail_after(HANDSHAKE_TIMEOUT):
                # Only the timeout for successful handshakes has to be checked as a failure during
                # handshake will make the service as a whole fail.
                await handshake_successful_event.wait()
        except trio.TooSlowError as too_slow_error:
            self.logger.warning("Handshake with %s has timed out", encode_hex(self.remote_node_id))
            raise HandshakeFailure("Handshake has timed out") from too_slow_error

    #
    # Handshake states
    # 
Example #25
Source File: mount.py    From s3ql with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, backend_pool, param, db, interval):
        super().__init__()
        self.backend_pool = backend_pool
        self.param = param
        self.db = db
        self.interval = interval
        self.db_mtime = os.stat(db.file).st_mtime
        self.event = trio.Event()
        self.quit = False

        # Can't assign in constructor, because Operations instance needs
        # access to self.event as well.
        self.fs = None 
Example #26
Source File: mount.py    From s3ql with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, block_cache):
        super().__init__()
        self.block_cache = block_cache
        self.stop_event = trio.Event() 
Example #27
Source File: test_connection.py    From trio-websocket with MIT License 5 votes vote down vote up
def test_read_messages_after_remote_close(nursery):
    '''
    When the remote endpoint closes, the local endpoint can still read all
    of the messages sent prior to closing. Any attempt to read beyond that will
    raise ConnectionClosed.

    This test also exercises the configuration of the queue size.
    '''
    server_closed = trio.Event()

    async def handler(request):
        server = await request.accept()
        async with server:
            await server.send_message('1')
            await server.send_message('2')
        server_closed.set()

    server = await nursery.start(
        partial(serve_websocket, handler, HOST, 0, ssl_context=None))

    # The client needs a message queue of size 2 so that it can buffer both
    # incoming messages without blocking the reader task.
    async with open_websocket(HOST, server.port, '/', use_ssl=False,
            message_queue_size=2) as client:
        await server_closed.wait()
        assert await client.get_message() == '1'
        assert await client.get_message() == '2'
        with pytest.raises(ConnectionClosed):
            await client.get_message() 
Example #28
Source File: conftest.py    From parsec-cloud with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self._thread = None
        self._trio_token = None
        self._request_queue = queue.Queue()
        self._test_result = futures.Future()
        self._job_scheduler = QtToTrioJobScheduler()

        # State events
        self._stopping = None
        self._started = threading.Event() 
Example #29
Source File: context.py    From hypercorn with MIT License 5 votes vote down vote up
def clear(self) -> None:
        self._event = trio.Event() 
Example #30
Source File: tcp_server.py    From hypercorn with MIT License 5 votes vote down vote up
def __init__(self) -> None:
        self._event = trio.Event()