예제 #1
0
파일: asyncoro.py 프로젝트: MurtAlsa/mpyc
    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)
예제 #2
0
파일: server.py 프로젝트: MaxwellRebo/cess
    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)
예제 #3
0
    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)
예제 #4
0
    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))
예제 #5
0
    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)
예제 #6
0
    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
예제 #7
0
 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)
예제 #8
0
    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
예제 #9
0
파일: loop.py 프로젝트: pfreixes/qloop
    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
예제 #10
0
    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
예제 #11
0
 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
예제 #12
0
 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)
예제 #13
0
 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)
예제 #14
0
 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)
예제 #15
0
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()
예제 #16
0
파일: pool.py 프로젝트: tzoiker/aiomisc
 def __task_add(self, task: asyncio.Task) -> None:
     task.add_done_callback(self.__task_store.remove)
     self.__task_store.add(task)