def _loop_mgr(loop: asyncio.AbstractEventLoop): asyncio.set_event_loop(loop) if not loop.is_running(): loop.run_forever() # If we reach here, the loop was stopped. # We should gather any remaining tasks and finish them. pending = asyncio.all_tasks(loop) if pending: loop.run_until_complete(asyncio.gather(*pending)) if not loop.is_running(): loop.close()
def start_read(self, event_loop: asyncio.AbstractEventLoop) -> None: self.event_loop = event_loop if event_loop.is_running(): self.event_reader_task = asyncio.run_coroutine_threadsafe( self.read_events(), event_loop) else: self.event_reader_task = event_loop.create_task(self.read_events())
async def invest_api_server( loop: asyncio.AbstractEventLoop, invest_api_session: orm.Session, ) -> AsyncIterator: assert loop.is_running() db_url = str(invest_api_session.bind.url) config = { "db": { "pool": { "dsn": db_url, "min_size": 1, "max_size": 5, }, "logger": { "name": "db", }, }, } host = LOCALHOST port = unused_port() async with invest_api.create_tcp_server(host, port, config) as url: yield url
def _loop(loop: asyncio.AbstractEventLoop): asyncio.set_event_loop(loop) if not loop.is_running() or loop.is_closed(): loop.run_forever() pending = asyncio.all_tasks(loop=loop) if pending: loop.run_until_complete(asyncio.gather(*pending))
def start_event_loop(loop: AbstractEventLoop) -> AbstractEventLoop: """启动事件循环""" # 如果事件循环未运行,则创建后台线程来运行 if not loop.is_running(): thread = Thread(target=run_event_loop, args=(loop, )) thread.daemon = True thread.start()
def emit_after( self, delay: float, name: str, args: tuple = (), kwargs: dict = None, loop: asyncio.AbstractEventLoop = None, retries: int = None, ): """ Emit an event after a given period of time. :param delay: a float of the time (in seconds) you want to delay the call :param name: event name :param args: additional event arguments :param kwargs: addition event keyword arguments :param loop: asyncio event loop from which you want the event to be emitted """ # event = self._events.get(name) loop = loop or self.loop assert loop.is_running() loop.call_later(delay, self.emit, name, args=args, kwargs=kwargs, retries=retries, loop=loop)
def testNoAsync(loop: asyncio.AbstractEventLoop): async def asyncfunc(): await asyncio.sleep(5) if not loop.is_running(): loop = asyncio.new_event_loop() loop.run_until_complete(asyncfunc()) else: asyncio.create_task(asyncfunc())
async def _do_loop(self, loop: AbstractEventLoop): while loop.is_running(): try: sent_from, message = self.in_queue.get(timeout=0.1) loop.create_task( self._handle_message(message, sent_from, self._state)) except Empty: pass await asyncio.sleep(0)
def loop_exception_handler(loop: asyncio.AbstractEventLoop, context: Dict[str, Any]) -> None: """A custom error handler for the loop, which stops the loop before continuing to the default handler """ logging.error("Terminating loop due to error") if loop.is_running(): loop.stop() loop.default_exception_handler(context)
async def _dispatch_messages(self, loop: AbstractEventLoop): while loop.is_running(): actors = { actor_id: actor for actor_id, actor in self._actors.items() if actor.alive.value } for actor_id, actor in actors.items(): self._fetch_single_message(actor_id, actor.out_queue) self._actors = actors await asyncio.sleep(0)
def test_cleanup_with_exception( self, event_loop: asyncio.AbstractEventLoop, log_records: LogRecordsType, ) -> None: event_loop.create_task(raising_task()) assert len(asyncio.all_tasks(event_loop)) == 6 event_loop.run_until_complete(shutdown()) assert not event_loop.is_running() last_log_record = log_records()[-1] assert last_log_record.levelno == logging.ERROR assert "Exception for testing" in last_log_record.message
def start_client(self, server_uri, loop: asyncio.AbstractEventLoop = None, wait_on_reader=True): """ Start the client (spinning out self.run as an asyncio task) Args: server_uri (str): uri to server pubsub-endpoint (e.g. 'http://localhost/pubsub') loop (asyncio.AbstractEventLoop, optional): event loop to run on. Defaults to asyncio.get_event_loop(). wait_on_reader (bool, optional): Wait on task reading from server. Defaults to True. """ loop = loop or asyncio.get_event_loop() # If the loop hasn't started yet - take over if not loop.is_running(): loop.run_until_complete(self.run(server_uri, wait_on_reader)) # Otherwise else: self._run_task = asyncio.create_task(self.run(server_uri, wait_on_reader))
def block(coroutine: Coroutine, loop: asyncio.AbstractEventLoop, *, timeout): if loop.is_running(): coroutine.close() raise CannotBlockHere( "It appears you have tried to use a blocking API method " "from within an event loop. Unfortunately this is unsupported. " "Instead, use the async version of the method. This commonly " "occurs when calling bus methods from within a bus event listener. " "In this case the only option is to define you listeners as async." ) try: val = loop.run_until_complete( asyncio.wait_for(coroutine, timeout=timeout, loop=loop)) except Exception as e: # The intention here is to get sensible stack traces from exceptions within blocking calls raise e return val
async def _monitor_lag(self, loop: AbstractEventLoop): log.info("Monitoring async lag started") while loop.is_running(): start = loop.time() await sleep(self._interval) # The closer this gap is to our intended sleep time # the less load the system is under. Large gaps mean # the running loop is dealing with a lot of work time_slept = loop.time() - start self.lag = time_slept - self._interval log.debug(f"Current async lag (ms): {self.lag * 1000}") tasks = [task for task in Task.all_tasks(loop) if not task.done()] self.active_tasks = len(tasks) log.debug(f"Active tasks: {self.active_tasks}") self._warn(tasks) log.info("Monitoring async lag stopped")
def run_or_wait(future: asyncio.Future, loop: asyncio.AbstractEventLoop): if not loop.is_running(): loop.run_until_complete(future) else: list(future.__await__())
def test_cleanup_no_exception( self, event_loop: asyncio.AbstractEventLoop) -> None: assert len(asyncio.all_tasks(event_loop)) == 5 event_loop.run_until_complete(shutdown()) assert not event_loop.is_running()