Example #1
0
def task_factory(loop: asyncio.BaseEventLoop, coro: typing.Coroutine):
    """
    Task factory for implementing context processor

    :param loop:
    :param coro:
    :return: new task
    :rtype: :obj:`asyncio.Task`
    """
    # Is not allowed when loop is closed.
    if loop.is_closed():
        raise RuntimeError('Event loop is closed.')

    task = asyncio.Task(coro, loop=loop)

    # Hide factory
    if task._source_traceback:
        del task._source_traceback[-1]

    try:
        task.context = asyncio.Task.current_task().context.copy()
    except AttributeError:
        task.context = {CONFIGURED: True}

    return task
Example #2
0
def task_factory(loop: asyncio.BaseEventLoop, coroutine: Coroutine):
    if loop.is_closed():
        raise RuntimeError('Event loop is closed')

    task = asyncio.Task(coroutine, loop=loop)

    try:
        task.context = asyncio.Task.current_task().context.copy()
    except AttributeError:
        task.context = {CONFIGURED: True}

    return task