def typed_asyncoro(*args, **kwargs): runtime._pc_level += 1 coro = func(*args, **kwargs) if rettype: decl = returnType(rettype, wrap=False) else: try: decl = coro.send(None) except StopIteration as exc: runtime._pc_level -= 1 return exc.value except Exception: runtime._pc_level -= 1 raise if runtime.options.no_async: while True: try: coro.send(None) except StopIteration as exc: runtime._pc_level -= 1 if decl is not None: __reconcile(decl, exc.value) return decl except Exception: runtime._pc_level -= 1 raise if pc: coro = _wrap_in_coro(_ProgramCounterWrapper(runtime, coro)) task = Task(coro, loop=runtime._loop) task.add_done_callback(lambda t: _reconcile(decl, t)) return _ncopy(decl)
def _accept_client(self, client_reader, client_writer): """manage new client connections""" task = Task(self._handle_client(client_reader, client_writer)) self.clients[task] = (client_reader, client_writer) def client_done(task): del self.clients[task] task.add_done_callback(client_done)
def reenter_after(self, target: Task, action: Callable): msg = self._message def act(): action() return None cont = Continuation(act, msg) target.add_done_callback(lambda _: self.my_self.send_system_message(cont))
def cancel_test_on_exception(task: asyncio.Task): def maybe_cancel_clbk(t: asyncio.Task): exception = t.exception() if exception is None: return for task in asyncio.all_tasks(): coro = task.get_coro() if coro.__qualname__ == request.function.__qualname__: task.cancel() return task.add_done_callback(maybe_cancel_clbk)
def add_active(self, task: asyncio.Task, task_complete: Callable = None) -> asyncio.Task: """ Register an active async task with an optional completion callback. Args: task: The asyncio task instance task_complete: An optional callback to run on completion """ self.active_tasks.append(task) task.add_done_callback( lambda fut: self.completed_task(task, task_complete)) return task
def addFuture(self, future: Task, futRef: str): def onComplete(fut): try: self.pendingTasks.discard(fut) fut.result() except asyncio.CancelledError: logger.info(f"{self.id} {futRef} was cancelled") except Exception: kwArgs = {} if self.policy.logdetail == "VERBOSE": kwArgs["exc_info"] = True logger.error(f"{self.id} {futRef} errored", **kwArgs) future.add_done_callback(onComplete) self.pendingTasks.add(future)
def register_sound(self, group_index: int, sound_index: int, task: asyncio.Task): """ Register that the given task belongs to the given group_index and sound_index. Will automatically unregister the task after it has finished. Raises a `RuntimeError` if the task is already done. Finished tasks are not allowed to be registered. """ logger.debug( f"Registering task for group={group_index}, sound={sound_index}") key = self._get_sound_key(group_index, sound_index) if task.done(): raise RuntimeError( f"Task for group={group_index}, sound={sound_index} is done, but was registered!" ) task.add_done_callback( functools.partial(self._unregister_sound, group_index, sound_index)) self.sound_to_task[key] = task
def _inherit_queue(self, coro): """Create a new task inheriting the partition assigned to the current task. If there is no current task, or the curren task does not have any queue will be assinged to the root one. Return a task object """ task = Task(coro, loop=self) task.partition = _find_partition(self) if task._source_traceback: del task._source_traceback[-1] self._partitions[task.partition].add_task(task) task.add_done_callback(self._partitions[task.partition].remove_task, task) return task
def add_active( self, task: asyncio.Task, task_complete: Callable = None, ident: str = None, timing: dict = None, ) -> asyncio.Task: """ Register an active async task with an optional completion callback. Args: task: The asyncio task instance task_complete: An optional callback to run on completion ident: A string identifer for the task timing: An optional dictionary of timing information """ self.active_tasks.append(task) task.add_done_callback(lambda fut: self.completed_task( task, task_complete, ident, timing)) self.total_started += 1 return task
def register_cancellable_task(self, task: asyncio.Task): self._cancellable_tasks.add(task) task.add_done_callback(lambda t: self._cancellable_tasks.discard(t)) return task
def remember_task(self, task: asyncio.Task) -> None: """Remember a scheduled asyncio task, in case it needs to be cancelled """ self.running_tasks.append(task) task.add_done_callback(self.forget_task)
def __task_add(self, task: asyncio.Task) -> None: self._statistic.task_added += 1 task.add_done_callback(self.__task_store.remove) self.__task_store.add(task)
def task(self, task: Task): assert isinstance(task, Task), f"{task} is not a task" self._awaitable = task task.add_done_callback(self._run_callbacks)
def wait_task_completed(task: asyncio.Task): if task.done() or task.cancelled(): return signal = Event() task.add_done_callback(lambda x: signal.set()) signal.wait()
def __task_add(self, task: asyncio.Task) -> None: task.add_done_callback(self.__task_store.remove) self.__task_store.add(task)