def _task_info(task) -> Dict: return { "id": id(task), "name": task.get_coro().__qualname__, "task_name": task.get_name(), "done": task.done(), "cancelled": task.cancelled(), "state": task._state, "details": { "locals": inspect.getcoroutinelocals(task) }, }
def _auto_ident(coro: Coroutine[Any, Any, T_Result]) -> str: name = getattr(coro, '__qualname__', getattr(coro, '__name__')) try: hsh = hash(tuple(inspect.getcoroutinelocals(coro).items())) except TypeError: raise TypeError( 'Unable to auto calculate identity for coroutine because using' ' unhashable arguments, you should set `ident` manual like:' '\n\tawait async_reduce({}(...), ident="YOU-IDENT-FOR-THAT")' ''.format(getattr(coro, '__name__'))) return '_auto_ident:{}:{}'.format(name, hsh)
def stop(self, loop: asyncio.AbstractEventLoop = None): """Stop running all tasks associated with this schedule""" if loop is None: loop = get_loop() self.end_on = datetime.datetime.now() try: # Assume only one task runs this schedule or stop cancels all tasks with this schedule. for task in asyncio.all_tasks(loop): try: if inspect.getcoroutinelocals( task.get_coro())['self'] == self: task.cancel() except: pass except: pass
def wrapper(*args, **kwargs): task = TimedTask(func(*args, **kwargs)) stack_function = [] outer_frames = inspect.getouterframes(inspect.currentframe()) for f in outer_frames: traceback = inspect.getframeinfo(f.frame) stack_function.append(traceback.function) print(inspect.getframeinfo(f.frame)) print("Stack functions", stack_function) print("\n\n") run_traceback = inspect.getframeinfo(inspect.currentframe().f_back) local_vars = str(getcoroutinelocals(task._coro))[:200] task.add_done_callback( functools.partial(_results_handler, handlers, local_vars, run_traceback)) return (yield from task)
async def cancel_subgraph(self, node): """ Cancels the task related to the current node as well as the tasks related to every child of that node. Also removes them from the worker's internal in_process queue. """ # dependents = self.workflow.get_dependents(node) cancelled_tasks = set() self.cancelled.append(node.id_) to_cancel = await self.cancel_helper(node, [node.id_]) for task in self.scheduling_tasks: for _, arg in getcoroutinelocals(task._coro).items(): if isinstance(arg, Node): if arg.id_ in to_cancel: self.in_process.pop(arg.id_) self.accumulator[arg.id_] = None self.cancelled.append(arg.id_) task.cancel() cancelled_tasks.add(task) await asyncio.gather(*cancelled_tasks, return_exceptions=True)
def __init__(self, coro, *, loop=None): super().__init__(coro, loop=loop) self.holder = inspect.getcoroutinelocals(coro).get('self') try: self.uid = self.holder.uid except AttributeError: self.uid = str(uuid4()) self._broker = get_broker(self._loop) self._in_progress = False self._template = None self._workflow = None self._source = None self._start = None self._end = None self._inputs = None self._outputs = None self._queue = asyncio.Queue(loop=self._loop) if self.holder: self.holder.queue = self._queue # A 'committed' task is a pending task not suspended self._committed = asyncio.Event() self._committed.set()
def __init__(self, coro, *, loop=None): super().__init__(coro, loop=loop) self.holder = inspect.getcoroutinelocals(coro).get('self') try: self.uid = self.holder.uid except AttributeError: self.uid = str(uuid4()) self._broker = get_broker(self._loop) self._in_progress = False self._template = None self._workflow = None self._source = None self._start = None self._end = None self._inputs = None self._outputs = None self._queue = asyncio.Queue(loop=self._loop) if self.holder: self.holder.queue = self._queue # A 'committed' task is a pending task not suspended self._committed = asyncio.Event() self._committed.set() self._timed_out = False
def main(): ########################################################################## # native coroutine example ########################################################################## print(str.format( 'coroutine_function function is coroutine function => {}', inspect.iscoroutinefunction(coroutine_function) )) print(str.format( 'coroutine_function type => {}', type(coroutine_function) )) # create native python coroutine coroutine = coroutine_function() print(str.format( 'coroutine is coroutine => {}', inspect.iscoroutine(coroutine) )) print(str.format('coroutine type => {}', type(coroutine))) print(str.format( 'coroutine state => {}', inspect.getcoroutinestate(coroutine) )) print(str.format( 'coroutine locals => {}', inspect.getcoroutinelocals(coroutine) )) ########################################################################## # generator example ########################################################################## print(str.format( 'generator_function function is generator function => {}', inspect.isgeneratorfunction(generator_function) )) print(str.format( 'generator_function type => {}', type(generator_function) )) # create generator object generator = generator_function() print(str.format( 'generator is generator object => {}', inspect.isgenerator(generator) )) print(str.format('generator object type => {}', type(generator))) print(str.format( 'generator object state => {}', inspect.getgeneratorstate(generator) )) # iterate generator generator_result1 = next(generator) print(str.format( 'generator object last iterate result => {}', generator_result1 )) print(str.format( 'generator object state => {}', inspect.getgeneratorstate(generator) )) try: generator_result2 = next(generator) print(str.format( 'generator object last iterate result => {}', generator_result2 )) except StopIteration: print(str.format('[#] generator object iterated')) print(str.format( 'generator object state => {}', inspect.getgeneratorstate(generator) )) ########################################################################## # ordinary function example ########################################################################## print(str.format( 'ordinary_function() is ordinary function: {}', inspect.isfunction(ordinary_function) ))