Python trio.TASK_STATUS_IGNORED Examples
The following are 26
code examples of trio.TASK_STATUS_IGNORED().
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: lifespan.py From hypercorn with MIT License | 6 votes |
def handle_lifespan( self, *, task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED ) -> None: task_status.started() scope = {"type": "lifespan", "asgi": {"spec_version": "2.0"}} try: await invoke_asgi(self.app, scope, self.asgi_receive, self.asgi_send) except LifespanFailure: # Lifespan failures should crash the server raise except Exception: self.supported = False await self.config.log.exception( "ASGI Framework Lifespan error, continuing without Lifespan support" ) finally: await self.app_send_channel.aclose() await self.app_receive_channel.aclose()
Example #2
Source File: full.py From trinity with MIT License | 6 votes |
def _manage_peers( self, task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED ) -> None: task_status.started() async with self._peer_updates: async for peer_id, update in self._peer_updates: if isinstance(update, Status): sync_request = self._determine_sync_request(peer_id, update) if sync_request: await self._sync_notifier.send(sync_request) elif isinstance(update, GoodbyeReason): self.logger.debug( "recv'd goodbye from %s with reason: %s", peer_id, update ) await self._host.drop_peer(peer_id) elif isinstance(update, MetaDataSeqNumber): # TODO: track peers and their metadata self.logger.debug( "recv'd ping from %s with seq number: %s", peer_id, update )
Example #3
Source File: _impl.py From trio-websocket with MIT License | 6 votes |
def run(self, *, task_status=trio.TASK_STATUS_IGNORED): ''' Start serving incoming connections requests. This method supports the Trio nursery start protocol: ``server = await nursery.start(server.run, …)``. It will block until the server is accepting connections and then return a :class:`WebSocketServer` object. :param task_status: Part of the Trio nursery start protocol. :returns: This method never returns unless cancelled. ''' async with trio.open_nursery() as nursery: serve_listeners = partial(trio.serve_listeners, self._handle_connection, self._listeners, handler_nursery=self._handler_nursery) await nursery.start(serve_listeners) logger.debug('Listening on %s', ','.join([str(l) for l in self.listeners])) task_status.started(self) await trio.sleep_forever()
Example #4
Source File: __init__.py From starbelly with MIT License | 6 votes |
def run(self, *, task_status=trio.TASK_STATUS_IGNORED): ''' Run the websocket server. To ensure that the server is ready, call ``await nursery.start(server.run)``. :returns: Runs until cancelled. ''' logger.info('Starting server on %s:%d', self._host, self._port) async with trio.open_nursery() as nursery: serve_fn = partial(serve_websocket, self._handle_connection, self._host, self._port, ssl_context=None, handler_nursery=nursery) server = await nursery.start(serve_fn, name='Connection Listener') self._port = server.port task_status.started() logger.info('Server stopped')
Example #5
Source File: trio_thread.py From parsec-cloud with GNU Affero General Public License v3.0 | 6 votes |
def _start(self, *, task_status=trio.TASK_STATUS_IGNORED): assert not self.started.is_set() self._trio_token = trio.hazmat.current_trio_token() self._send_job_channel, recv_job_channel = trio.open_memory_channel(1) try: async with trio.open_service_nursery() as nursery, recv_job_channel: self._cancel_scope = nursery.cancel_scope self.started.set() task_status.started() while True: job = await recv_job_channel.receive() assert job.status is None await nursery.start(job._run_fn) finally: self._stopped.set()
Example #6
Source File: job.py From starbelly with MIT License | 6 votes |
def run(self, *, task_status=trio.TASK_STATUS_IGNORED): ''' Run the crawl manager. You should call ``await nursery.start(crawl_manager.run)`` to ensure that the crawl manager is ready before calling any of its job methods. :returns: This function runs until cancelled. ''' max_sequence = await self._db.get_max_sequence() self._sequence = itertools.count(start=max_sequence + 1) logger.info('%r Sequence initialized to %s', self, max_sequence + 1) async with trio.open_nursery() as nursery: self._nursery = nursery task_status.started() await trio.sleep_forever()
Example #7
Source File: test_authenticated_conn.py From parsec-cloud with GNU Affero General Public License v3.0 | 6 votes |
def test_monitor_crash(running_backend, event_bus, alice, during_bootstrap): async def _bad_monitor(*, task_status=trio.TASK_STATUS_IGNORED): if during_bootstrap: raise RuntimeError("D'oh !") task_status.started() await trio.sleep(0) raise RuntimeError("D'oh !") conn = BackendAuthenticatedConn( alice.organization_addr, alice.device_id, alice.signing_key, event_bus ) with event_bus.listen() as spy: conn.register_monitor(_bad_monitor) async with conn.run(): await spy.wait_with_timeout( CoreEvent.BACKEND_CONNECTION_CHANGED, {"status": BackendConnStatus.CRASHED, "status_exc": spy.ANY}, ) assert conn.status == BackendConnStatus.CRASHED # Test command not possible with pytest.raises(BackendNotAvailable) as exc: await conn.cmds.ping() assert str(exc.value) == "Backend connection manager has crashed: D'oh !"
Example #8
Source File: test_utils.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def job(fail=0, task_status=trio.TASK_STATUS_IGNORED): await trio.sleep(1) if fail < 0: raise RuntimeError("Oops") success = set() task_status.started(success) await trio.sleep(1) if fail > 0: raise RuntimeError("Oops") success.add(1)
Example #9
Source File: trio_utils.py From trinity with MIT License | 5 votes |
def serve( self, task_status: TaskStatus[int] = trio.TASK_STATUS_IGNORED ) -> None: async with trio.open_nursery() as nursery: # NOTE: `mypy` does not like the typing here but this # should type check... listeners: Sequence[trio.SocketListener] = await nursery.start( trio.serve_tcp, self.handler, self.port # type: ignore ) self.port = int(listeners[0].socket.getsockname()[1]) task_status.started(self.port)
Example #10
Source File: full.py From trinity with MIT License | 5 votes |
def _manage_sync_requests( self, task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED ) -> None: task_status.started() async with self._sync_requests: async for request in self._sync_requests: async for block in self._host.get_blocks_by_range( request.peer_id, request.start_slot, request.count ): imported = self.on_block(block) if not imported: self.logger.warning( "an issue with sync of this block %s", block )
Example #11
Source File: full.py From trinity with MIT License | 5 votes |
def _run_host( self, task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED ) -> None: host = self._host listen_maddr = self._p2p_maddr try: # NOTE the following code relies on knowledge of the internals here... # We ideally want better encapsulation but it looks like it will # involve separating out the ``Service`` abstraction from the # various ``libp2p`` abstractions... async with host.run(listen_addrs=(listen_maddr,)): task_status.started() self.logger.info("peer listening at %s", listen_maddr) async with background_trio_service(host._gossiper.pubsub): async with background_trio_service(host._gossiper.gossipsub): await self._connect_preferred_nodes() # NOTE: need to connect *some* peers first before # subscribing to gossip # FIXME: can likely move this inside ``host``. await host.subscribe_gossip_channels() await self._handle_gossip() await host.unsubscribe_gossip_channels() except Exception as e: # TODO: likely want to catch exceptions in a more granular way # and restart the host... self.logger.exception(e)
Example #12
Source File: full.py From trinity with MIT License | 5 votes |
def _run_validator_api( self, task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED ) -> None: server = self._validator_api_server async with trio.open_nursery() as nursery: self.validator_api_port = await nursery.start(server.serve) self.logger.info( "validator HTTP API server listening on %d", self.validator_api_port ) task_status.started()
Example #13
Source File: full.py From trinity with MIT License | 5 votes |
def _iterate_clock( self, task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED ) -> None: task_status.started() async for tick in self._clock: if tick.count == 0: self.logger.debug( "slot %d [number %d in epoch %d]", tick.slot, tick.slot_in_epoch(self._eth2_config.SLOTS_PER_EPOCH), tick.epoch, ) self._chain.on_tick(tick)
Example #14
Source File: __init__.py From hypercorn with MIT License | 5 votes |
def serve( app: ASGIFramework, config: Config, *, shutdown_trigger: Optional[Callable[..., Awaitable[None]]] = None, task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED, ) -> None: """Serve an ASGI framework app given the config. This allows for a programmatic way to serve an ASGI framework, it can be used via, .. code-block:: python trio.run(partial(serve, app, config)) It is assumed that the event-loop is configured before calling this function, therefore configuration values that relate to loop setup or process setup are ignored. Arguments: app: The ASGI application to serve. config: A Hypercorn configuration object. shutdown_trigger: This should return to trigger a graceful shutdown. """ if config.debug: warnings.warn("The config `debug` has no affect when using serve", Warning) if config.workers != 1: warnings.warn("The config `workers` has no affect when using serve", Warning) await worker_serve(app, config, shutdown_trigger=shutdown_trigger, task_status=task_status)
Example #15
Source File: common.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def call_with_control(controlled_fn, *, task_status=trio.TASK_STATUS_IGNORED): controller = CallController() async def _started_cb(**kwargs): controller.__dict__.update(kwargs) task_status.started(controller) await controller.need_stop.wait() try: await controlled_fn(_started_cb) finally: controller.stopped.set()
Example #16
Source File: test_transport.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def serve_tcp_testbed(unused_tcp_port): async def _serve_tcp_testbed(*conns): host = "127.0.0.1" send_channel, receive_channel = trio.open_memory_channel(0) async def _serve_client(stream): server_fn = await receive_channel.receive() transport = await Transport.init_for_server(stream) await server_fn(transport) async def _store_handlers(*, task_status=trio.TASK_STATUS_IGNORED): async with trio.open_service_nursery() as handler_nursery: task_status.started(handler_nursery) await trio.sleep_forever() async with trio.open_service_nursery() as nursery: handler_nursery = await nursery.start(_store_handlers) await nursery.start( partial( trio.serve_tcp, _serve_client, unused_tcp_port, handler_nursery=handler_nursery ) ) assert not handler_nursery.child_tasks for client_fn, server_fn in conns: stream = await trio.open_tcp_stream(host, unused_tcp_port) await send_channel.send(server_fn) transport = await Transport.init_for_client(stream, host) await client_fn(transport) await trio.testing.wait_all_tasks_blocked() # No pending connections should remain assert not handler_nursery.child_tasks await send_channel.aclose() nursery.cancel_scope.cancel() return _serve_tcp_testbed
Example #17
Source File: trio_thread.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _run_fn(self, *, task_status=trio.TASK_STATUS_IGNORED): with trio.CancelScope() as self.cancel_scope: task_status.started() self._started.set() try: if not iscoroutinefunction(self._fn): result = self._fn(*self._args, **self._kwargs) else: result = await self._fn(*self._args, **self._kwargs) self.set_result(result) except Exception as exc: self.set_exception(exc) except trio.Cancelled as exc: self.set_cancelled(exc) raise except trio.MultiError as exc: cancelled_errors, other_exceptions = split_multi_error(exc) if other_exceptions: self.set_exception(other_exceptions) else: self.set_cancelled(cancelled_errors) if cancelled_errors: raise cancelled_errors
Example #18
Source File: userfs.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _instantiate_workspace_storage(self, workspace_id: EntryID) -> WorkspaceStorage: path = self.path / str(workspace_id) async def workspace_storage_task(task_status=trio.TASK_STATUS_IGNORED): async with WorkspaceStorage.run(self.device, path, workspace_id) as workspace_storage: task_status.started(workspace_storage) await trio.sleep_forever() return await self._workspace_storage_nursery.start(workspace_storage_task)
Example #19
Source File: handler.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _run_connections(self, task_status=trio.TASK_STATUS_IGNORED): async with triopg.create_pool( self.url, min_size=self.min_connections, max_size=self.max_connections ) as self.pool: # This connection is dedicated to the notifications listening, so it # would only complicate stuff to include it into the connection pool async with triopg.connect(self.url) as self.notification_conn: await self.notification_conn.add_listener("app_notification", self._on_notification) task_status.started() await trio.sleep_forever()
Example #20
Source File: utils.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def wrap_task(cls, corofn, *args, task_status=trio.TASK_STATUS_IGNORED): status = cls() try: async with trio.open_service_nursery() as nursery: status._set_cancel_scope(nursery.cancel_scope) value = await nursery.start(corofn, *args) status._set_started_value(value) task_status.started(status) finally: status._set_finished()
Example #21
Source File: udp_server.py From hypercorn with MIT License | 5 votes |
def run( self, task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED ) -> None: task_status.started() while True: data, address = await self.socket.recvfrom(MAX_RECV) await self.protocol.handle(RawData(data=data, address=address))
Example #22
Source File: tcp_server.py From hypercorn with MIT License | 5 votes |
def _call_later( timeout: float, callback: Callable, task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED, ) -> None: cancel_scope = trio.CancelScope() task_status.started(cancel_scope) with cancel_scope: await trio.sleep(timeout) cancel_scope.shield = True await callback()
Example #23
Source File: test_fuse.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def test_mountpoint_path_already_in_use_concurrent_with_mountpoint( monkeypatch, base_mountpoint, running_backend, alice_user_fs, alice2_user_fs ): # Create a workspace and make it available in two devices wid = await alice_user_fs.workspace_create("w") await alice_user_fs.sync() await alice2_user_fs.sync() mountpoint_path = base_mountpoint.absolute() / "w" async def _mount_alice2_w_mountpoint(*, task_status=trio.TASK_STATUS_IGNORED): async with mountpoint_manager_factory( alice2_user_fs, alice2_user_fs.event_bus, base_mountpoint ) as alice2_mountpoint_manager: await alice2_mountpoint_manager.mount_workspace(wid) task_status.started() await trio.sleep_forever() async with trio.open_service_nursery() as nursery: await nursery.start(_mount_alice2_w_mountpoint) # Here instead of checking the path can be used as a mountpoint, we # actually lead it into error async def _mocked_bootstrap_mountpoint(*args): trio_mountpoint_path = trio.Path(f"{mountpoint_path}") st_dev = (await trio_mountpoint_path.stat()).st_dev return mountpoint_path, st_dev monkeypatch.setattr( "parsec.core.mountpoint.fuse_runner._bootstrap_mountpoint", _mocked_bootstrap_mountpoint ) # Now we can start fuse async with mountpoint_manager_factory( alice_user_fs, alice_user_fs.event_bus, base_mountpoint ) as alice_mountpoint_manager: with pytest.raises(MountpointDriverCrash) as exc: await alice_mountpoint_manager.mount_workspace(wid) assert exc.value.args == (f"Fuse has crashed on {mountpoint_path}: EPERM",) # Test is over, stop alice2 mountpoint and exit nursery.cancel_scope.cancel()
Example #24
Source File: _impl.py From trio-websocket with MIT License | 4 votes |
def serve_websocket(handler, host, port, ssl_context, *, handler_nursery=None, message_queue_size=MESSAGE_QUEUE_SIZE, max_message_size=MAX_MESSAGE_SIZE, connect_timeout=CONN_TIMEOUT, disconnect_timeout=CONN_TIMEOUT, task_status=trio.TASK_STATUS_IGNORED): ''' Serve a WebSocket over TCP. This function supports the Trio nursery start protocol: ``server = await nursery.start(serve_websocket, …)``. It will block until the server is accepting connections and then return a :class:`WebSocketServer` object. Note that if ``host`` is ``None`` and ``port`` is zero, then you may get multiple listeners that have *different port numbers!* :param handler: An async function that is invoked with a request for each new connection. :param host: The host interface to bind. This can be an address of an interface, a name that resolves to an interface address (e.g. ``localhost``), or a wildcard address like ``0.0.0.0`` for IPv4 or ``::`` for IPv6. If ``None``, then all local interfaces are bound. :type host: str, bytes, or None :param int port: The port to bind to. :param ssl_context: The SSL context to use for encrypted connections, or ``None`` for unencrypted connection. :type ssl_context: ssl.SSLContext or None :param handler_nursery: An optional nursery to spawn handlers and background tasks in. If not specified, a new nursery will be created internally. :param int message_queue_size: The maximum number of messages that will be buffered in the library's internal message queue. :param int max_message_size: The maximum message size as measured by ``len()``. If a message is received that is larger than this size, then the connection is closed with code 1009 (Message Too Big). :param float connect_timeout: The number of seconds to wait for a client to finish connection handshake before timing out. :param float disconnect_timeout: The number of seconds to wait for a client to finish the closing handshake before timing out. :param task_status: Part of Trio nursery start protocol. :returns: This function runs until cancelled. ''' if ssl_context is None: open_tcp_listeners = partial(trio.open_tcp_listeners, port, host=host) else: open_tcp_listeners = partial(trio.open_ssl_over_tcp_listeners, port, ssl_context, host=host, https_compatible=True) listeners = await open_tcp_listeners() server = WebSocketServer(handler, listeners, handler_nursery=handler_nursery, message_queue_size=message_queue_size, max_message_size=max_message_size, connect_timeout=connect_timeout, disconnect_timeout=disconnect_timeout) await server.run(task_status=task_status)
Example #25
Source File: apiv1_authenticated.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def _manager_connect(self): async with self._acquire_transport(ignore_status=True, force_fresh=True) as transport: self._status = BackendConnStatus.INITIALIZING self._status_exc = None self._backend_connection_failures = 0 self.event_bus.send( CoreEvent.BACKEND_CONNECTION_CHANGED, status=self._status, status_exc=self._status_exc, ) logger.info("Backend online") await cmds.events_subscribe(transport) # Quis custodiet ipsos custodes? monitors_states = ["STALLED" for _ in range(len(self._monitors_cbs))] async def _wrap_monitor_cb(monitor_cb, idx, *, task_status=trio.TASK_STATUS_IGNORED): def _idle(): monitors_states[idx] = "IDLE" if all(state == "IDLE" for state in monitors_states): self._monitors_idle_event.set() def _awake(): monitors_states[idx] = "AWAKE" if self._monitors_idle_event.is_set(): self._monitors_idle_event = trio.Event() task_status.idle = _idle task_status.awake = _awake await monitor_cb(task_status=task_status) try: async with trio.open_service_nursery() as monitors_nursery: async with trio.open_service_nursery() as monitors_bootstrap_nursery: for idx, monitor_cb in enumerate(self._monitors_cbs): monitors_bootstrap_nursery.start_soon( monitors_nursery.start, partial(_wrap_monitor_cb, monitor_cb, idx) ) self._status = BackendConnStatus.READY self.event_bus.send( CoreEvent.BACKEND_CONNECTION_CHANGED, status=self._status, status_exc=None ) while True: rep = await cmds.events_listen(transport, wait=True) _handle_event(self.event_bus, rep) finally: # No more monitors are running self._monitors_idle_event.set()
Example #26
Source File: authenticated.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def _manager_connect(self): async with self._acquire_transport(ignore_status=True, force_fresh=True) as transport: self._status = BackendConnStatus.INITIALIZING self._status_exc = None self._backend_connection_failures = 0 self.event_bus.send( CoreEvent.BACKEND_CONNECTION_CHANGED, status=self._status, status_exc=self._status_exc, ) logger.info("Backend online") await cmds.events_subscribe(transport) # Quis custodiet ipsos custodes? monitors_states = ["STALLED" for _ in range(len(self._monitors_cbs))] async def _wrap_monitor_cb(monitor_cb, idx, *, task_status=trio.TASK_STATUS_IGNORED): def _idle(): monitors_states[idx] = "IDLE" if all(state == "IDLE" for state in monitors_states): self._monitors_idle_event.set() def _awake(): monitors_states[idx] = "AWAKE" if self._monitors_idle_event.is_set(): self._monitors_idle_event = trio.Event() task_status.idle = _idle task_status.awake = _awake await monitor_cb(task_status=task_status) try: async with trio.open_service_nursery() as monitors_nursery: async with trio.open_service_nursery() as monitors_bootstrap_nursery: for idx, monitor_cb in enumerate(self._monitors_cbs): monitors_bootstrap_nursery.start_soon( monitors_nursery.start, partial(_wrap_monitor_cb, monitor_cb, idx) ) self._status = BackendConnStatus.READY self.event_bus.send( CoreEvent.BACKEND_CONNECTION_CHANGED, status=self._status, status_exc=None ) while True: rep = await cmds.events_listen(transport, wait=True) _handle_event(self.event_bus, rep) finally: # No more monitors are running self._monitors_idle_event.set()