Співпрограми та завдання

У цьому розділі описано асинхронні API високого рівня для роботи з співпрограмами та завданнями.

Співпрограми

Source code: Lib/asyncio/coroutines.py


Coroutines, оголошений із синтаксисом async/await, є кращим способом написання асинхронних програм. Наприклад, наступний фрагмент коду друкує «hello», чекає 1 секунду, а потім друкує «world»:

>>> import asyncio

>>> async def main():
...     print('hello')
...     await asyncio.sleep(1)
...     print('world')

>>> asyncio.run(main())
hello
world

Зауважте, що простий виклик співпрограми не запланує її виконання:

>>> main()
<coroutine object main at 0x1053bb7c8>

To actually run a coroutine, asyncio provides the following mechanisms:

  • Функція asyncio.run() для запуску функції точки входу верхнього рівня «main()» (див. приклад вище).

  • Очікування співпрограми. Наступний фрагмент коду надрукує «привіт» після очікування 1 секунду, а потім надрукує «світ» після очікування ще 2 секунди:

    import asyncio
    import time
    
    async def say_after(delay, what):
        await asyncio.sleep(delay)
        print(what)
    
    async def main():
        print(f"started at {time.strftime('%X')}")
    
        await say_after(1, 'hello')
        await say_after(2, 'world')
    
        print(f"finished at {time.strftime('%X')}")
    
    asyncio.run(main())
    

    Очікуваний результат:

    started at 17:13:52
    hello
    world
    finished at 17:13:55
    
  • Функція asyncio.create_task() для одночасного запуску співпрограм як asyncio Tasks.

    Давайте змінимо наведений вище приклад і запустимо дві співпрограми say_after одночасно:

    async def main():
        task1 = asyncio.create_task(
            say_after(1, 'hello'))
    
        task2 = asyncio.create_task(
            say_after(2, 'world'))
    
        print(f"started at {time.strftime('%X')}")
    
        # Wait until both tasks are completed (should take
        # around 2 seconds.)
        await task1
        await task2
    
        print(f"finished at {time.strftime('%X')}")
    

    Зауважте, що очікуваний вихід тепер показує, що фрагмент працює на 1 секунду швидше, ніж раніше:

    started at 17:14:32
    hello
    world
    finished at 17:14:34
    
  • The asyncio.TaskGroup class provides a more modern alternative to create_task(). Using this API, the last example becomes:

    async def main():
        async with asyncio.TaskGroup() as tg:
            task1 = tg.create_task(
                say_after(1, 'hello'))
    
            task2 = tg.create_task(
                say_after(2, 'world'))
    
            print(f"started at {time.strftime('%X')}")
    
        # The await is implicit when the context manager exits.
    
        print(f"finished at {time.strftime('%X')}")
    

    The timing and output should be the same as for the previous version.

    Нове в версії 3.11: asyncio.TaskGroup.

очікування

Ми кажемо, що об’єкт є очікуваним об’єктом, якщо його можна використовувати у виразі await. Багато асинхронних API розроблено для прийняття очікуваних.

Існує три основних типи очікуваних об’єктів: співпрограми, Завдання та Ф’ючерси.

Співпрограми

Співпрограми Python є очікуваними, тому їх можна очікувати від інших співпрограм:

import asyncio

async def nested():
    return 42

async def main():
    # Nothing happens if we just call "nested()".
    # A coroutine object is created but not awaited,
    # so it *won't run at all*.
    nested()

    # Let's do it differently now and await it:
    print(await nested())  # will print "42".

asyncio.run(main())

Важливо

У цій документації термін «співпрограма» може використовуватися для двох тісно пов’язаних понять:

  • співпрограма: функція async def;

  • об’єкт співпрограми: об’єкт, повернутий викликом функції співпрограми.

завдання

Завдання використовуються для одночасного планування співпрограм.

Коли співпрограму загорнуто в Task із такими функціями, як asyncio.create_task(), співпрограма автоматично планується для незабаром:

import asyncio

async def nested():
    return 42

async def main():
    # Schedule nested() to run soon concurrently
    # with "main()".
    task = asyncio.create_task(nested())

    # "task" can now be used to cancel "nested()", or
    # can simply be awaited to wait until it is complete:
    await task

asyncio.run(main())

Ф’ючерси

Future — це спеціальний низькорівневий очікуваний об’єкт, який представляє кінцевий результат асинхронної операції.

Коли об’єкт Future очікується, це означає, що співпрограма чекатиме, поки Future не буде дозволено в іншому місці.

Майбутні об’єкти в asyncio потрібні для того, щоб код на основі зворотного виклику використовувався з async/await.

Зазвичай немає необхідності створювати об’єкти Future на рівні програми.

Майбутні об’єкти, іноді доступні бібліотеками та деякими асинхронними API, можна очікувати:

async def main():
    await function_that_returns_a_future_object()

    # this is also valid:
    await asyncio.gather(
        function_that_returns_a_future_object(),
        some_python_coroutine()
    )

Хорошим прикладом функції низького рівня, яка повертає об’єкт Future, є loop.run_in_executor().

Створення завдань

Source code: Lib/asyncio/tasks.py


asyncio.create_task(coro, *, name=None, context=None)

Загорніть coro coroutine в Task і заплануйте його виконання. Повернути об’єкт Task.

Якщо name не є None, воно встановлюється як назва завдання за допомогою Task.set_name().

An optional keyword-only context argument allows specifying a custom contextvars.Context for the coro to run in. The current context copy is created when no context is provided.

Завдання виконується в циклі, який повертає get_running_loop(), RuntimeError виникає, якщо в поточному потоці немає запущеного циклу.

Примітка

asyncio.TaskGroup.create_task() is a newer alternative that allows for convenient waiting for a group of related tasks.

Важливо

Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable «fire-and-forget» background tasks, gather them in a collection:

background_tasks = set()

for i in range(10):
    task = asyncio.create_task(some_coro(param=i))

    # Add task to the set. This creates a strong reference.
    background_tasks.add(task)

    # To prevent keeping references to finished tasks forever,
    # make each task remove its own reference from the set after
    # completion:
    task.add_done_callback(background_tasks.discard)

Нове в версії 3.7.

Змінено в версії 3.8: Додано параметр name.

Змінено в версії 3.11: Added the context parameter.

Task Cancellation

Tasks can easily and safely be cancelled. When a task is cancelled, asyncio.CancelledError will be raised in the task at the next opportunity.

It is recommended that coroutines use try/finally blocks to robustly perform clean-up logic. In case asyncio.CancelledError is explicitly caught, it should generally be propagated when clean-up is complete. asyncio.CancelledError directly subclasses BaseException so most code will not need to be aware of it.

The asyncio components that enable structured concurrency, like asyncio.TaskGroup and asyncio.timeout(), are implemented using cancellation internally and might misbehave if a coroutine swallows asyncio.CancelledError. Similarly, user code should not generally call uncancel. However, in cases when suppressing asyncio.CancelledError is truly desired, it is necessary to also call uncancel() to completely remove the cancellation state.

Task Groups

Task groups combine a task creation API with a convenient and reliable way to wait for all tasks in the group to finish.

class asyncio.TaskGroup

An asynchronous context manager holding a group of tasks. Tasks can be added to the group using create_task(). All tasks are awaited when the context manager exits.

Нове в версії 3.11.

create_task(coro, *, name=None, context=None)

Create a task in this task group. The signature matches that of asyncio.create_task().

Приклад:

async def main():
    async with asyncio.TaskGroup() as tg:
        task1 = tg.create_task(some_coro(...))
        task2 = tg.create_task(another_coro(...))
    print("Both tasks have completed now.")

The async with statement will wait for all tasks in the group to finish. While waiting, new tasks may still be added to the group (for example, by passing tg into one of the coroutines and calling tg.create_task() in that coroutine). Once the last task has finished and the async with block is exited, no new tasks may be added to the group.

The first time any of the tasks belonging to the group fails with an exception other than asyncio.CancelledError, the remaining tasks in the group are cancelled. No further tasks can then be added to the group. At this point, if the body of the async with statement is still active (i.e., __aexit__() hasn’t been called yet), the task directly containing the async with statement is also cancelled. The resulting asyncio.CancelledError will interrupt an await, but it will not bubble out of the containing async with statement.

Once all tasks have finished, if any tasks have failed with an exception other than asyncio.CancelledError, those exceptions are combined in an ExceptionGroup or BaseExceptionGroup (as appropriate; see their documentation) which is then raised.

Two base exceptions are treated specially: If any task fails with KeyboardInterrupt or SystemExit, the task group still cancels the remaining tasks and waits for them, but then the initial KeyboardInterrupt or SystemExit is re-raised instead of ExceptionGroup or BaseExceptionGroup.

If the body of the async with statement exits with an exception (so __aexit__() is called with an exception set), this is treated the same as if one of the tasks failed: the remaining tasks are cancelled and then waited for, and non-cancellation exceptions are grouped into an exception group and raised. The exception passed into __aexit__(), unless it is asyncio.CancelledError, is also included in the exception group. The same special case is made for KeyboardInterrupt and SystemExit as in the previous paragraph.

спить

coroutine asyncio.sleep(delay, result=None)

Блокувати на затримку секунд.

Якщо надано результат, він повертається абоненту після завершення співпрограми.

sleep() завжди призупиняє поточне завдання, дозволяючи виконувати інші завдання.

Встановлення затримки на 0 забезпечує оптимізований шлях для виконання інших завдань. Це може бути використано функціями, які довго виконуються, щоб уникнути блокування циклу подій протягом повної тривалості виклику функції.

Приклад співпрограми, що відображає поточну дату кожну секунду протягом 5 секунд:

import asyncio
import datetime

async def display_date():
    loop = asyncio.get_running_loop()
    end_time = loop.time() + 5.0
    while True:
        print(datetime.datetime.now())
        if (loop.time() + 1.0) >= end_time:
            break
        await asyncio.sleep(1)

asyncio.run(display_date())

Змінено в версії 3.10: Видалено параметр loop.

Одночасне виконання завдань

awaitable asyncio.gather(*aws, return_exceptions=False)

Запустіть waitable objects у послідовності aws одночасно.

Якщо будь-який awaitable у aws є співпрограмою, він автоматично запланований як завдання.

Якщо всі очікування виконано успішно, результатом буде зведений список повернутих значень. Порядок значень результатів відповідає порядку очікуваних значень у aws.

Якщо return_exceptions має значення False (за замовчуванням), перший викликаний виняток негайно поширюється на завдання, яке очікує на gather(). Інші очікування в послідовності aws не будуть скасовані і продовжуватимуть працювати.

Якщо return_exceptions має значення True, винятки обробляються так само, як успішні результати, і агрегуються в списку результатів.

Якщо gather() скасовано, усі надіслані очікування (які ще не завершені) також скасуються.

Якщо будь-яке завдання або майбутнє з послідовності aws скасовано, воно розглядається як викликане CancelledError – у цьому випадку виклик gather() не скасовується . Це робиться для того, щоб запобігти скасуванню одного поданого Завдання/Майбутнього, що спричинить скасування інших Завдань/Ф’ючерсів.

Примітка

A more modern way to create and run tasks concurrently and wait for their completion is asyncio.TaskGroup.

Приклад:

import asyncio

async def factorial(name, number):
    f = 1
    for i in range(2, number + 1):
        print(f"Task {name}: Compute factorial({number}), currently i={i}...")
        await asyncio.sleep(1)
        f *= i
    print(f"Task {name}: factorial({number}) = {f}")
    return f

async def main():
    # Schedule three calls *concurrently*:
    L = await asyncio.gather(
        factorial("A", 2),
        factorial("B", 3),
        factorial("C", 4),
    )
    print(L)

asyncio.run(main())

# Expected output:
#
#     Task A: Compute factorial(2), currently i=2...
#     Task B: Compute factorial(3), currently i=2...
#     Task C: Compute factorial(4), currently i=2...
#     Task A: factorial(2) = 2
#     Task B: Compute factorial(3), currently i=3...
#     Task C: Compute factorial(4), currently i=3...
#     Task B: factorial(3) = 6
#     Task C: Compute factorial(4), currently i=4...
#     Task C: factorial(4) = 24
#     [2, 6, 24]

Примітка

Якщо return_exceptions має значення False, скасування gather() після того, як його було позначено як виконане, не скасує жодних надісланих очікувань. Наприклад, gather можна позначити як виконане після передачі винятку викликаючому, отже, виклик gather.cancel() після перехоплення винятку (викликаного одним із очікуваних) від gather не скасує жодних інших очікуваних.

Змінено в версії 3.7: Якщо сам gather скасовано, скасування поширюється незалежно від return_exceptions.

Змінено в версії 3.10: Видалено параметр loop.

Застаріло починаючи з версії 3.10: Якщо не надано позиційних аргументів або не всі позиційні аргументи є Future-подібними об’єктами, і немає запущеного циклу подій, видається попередження про застаріле.

Захист від скасування

awaitable asyncio.shield(aw)

Захист очікуваного об’єкта від скасування.

Якщо aw є співпрограмою, вона автоматично запланована як завдання.

Заява:

task = asyncio.create_task(something())
res = await shield(task)

еквівалентно:

res = await something()

за винятком того, що якщо співпрограму, яка містить його, скасовується, Завдання, що виконується в something(), не скасовується. З точки зору something(), скасування не відбулося. Хоча його виклик все ще скасовано, тому вираз «чекати» все ще викликає CancelledError.

Якщо something() скасовано іншими засобами (тобто зсередини), це також скасує shield().

Якщо потрібно повністю ігнорувати скасування (не рекомендовано), функцію shield() слід поєднати з реченням try/except, як показано нижче:

task = asyncio.create_task(something())
try:
    res = await shield(task)
except CancelledError:
    res = None

Важливо

Save a reference to tasks passed to this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done.

Змінено в версії 3.10: Видалено параметр loop.

Застаріло починаючи з версії 3.10: Якщо aw не є Future-подібним об’єктом і немає запущеного циклу подій, видається попередження про застаріння.

Тайм-аути

asyncio.timeout(delay)

Return an asynchronous context manager that can be used to limit the amount of time spent waiting on something.

delay can either be None, or a float/int number of seconds to wait. If delay is None, no time limit will be applied; this can be useful if the delay is unknown when the context manager is created.

In either case, the context manager can be rescheduled after creation using Timeout.reschedule().

Приклад:

async def main():
    async with asyncio.timeout(10):
        await long_running_task()

If long_running_task takes more than 10 seconds to complete, the context manager will cancel the current task and handle the resulting asyncio.CancelledError internally, transforming it into a TimeoutError which can be caught and handled.

Примітка

The asyncio.timeout() context manager is what transforms the asyncio.CancelledError into a TimeoutError, which means the TimeoutError can only be caught outside of the context manager.

Example of catching TimeoutError:

async def main():
    try:
        async with asyncio.timeout(10):
            await long_running_task()
    except TimeoutError:
        print("The long operation timed out, but we've handled it.")

    print("This statement will run regardless.")

The context manager produced by asyncio.timeout() can be rescheduled to a different deadline and inspected.

class asyncio.Timeout(when)

An asynchronous context manager for cancelling overdue coroutines.

when should be an absolute time at which the context should time out, as measured by the event loop’s clock:

  • If when is None, the timeout will never trigger.

  • If when < loop.time(), the timeout will trigger on the next iteration of the event loop.

when() float | None

Return the current deadline, or None if the current deadline is not set.

reschedule(when: float | None)

Reschedule the timeout.

expired() bool

Return whether the context manager has exceeded its deadline (expired).

Приклад:

async def main():
    try:
        # We do not know the timeout when starting, so we pass ``None``.
        async with asyncio.timeout(None) as cm:
            # We know the timeout now, so we reschedule it.
            new_deadline = get_running_loop().time() + 10
            cm.reschedule(new_deadline)

            await long_running_task()
    except TimeoutError:
        pass

    if cm.expired():
        print("Looks like we haven't finished on time.")

Timeout context managers can be safely nested.

Нове в версії 3.11.

asyncio.timeout_at(when)

Similar to asyncio.timeout(), except when is the absolute time to stop waiting, or None.

Приклад:

async def main():
    loop = get_running_loop()
    deadline = loop.time() + 20
    try:
        async with asyncio.timeout_at(deadline):
            await long_running_task()
    except TimeoutError:
        print("The long operation timed out, but we've handled it.")

    print("This statement will run regardless.")

Нове в версії 3.11.

coroutine asyncio.wait_for(aw, timeout)

Зачекайте, поки aw awaitable завершиться з тайм-аутом.

Якщо aw є співпрограмою, вона автоматично запланована як завдання.

timeout може бути або None, або числом секунд для очікування з плаваючою точкою або int. Якщо timeout має значення None, блокуйте до завершення майбутнього.

If a timeout occurs, it cancels the task and raises TimeoutError.

Щоб уникнути завдання скасування, загорніть його в shield().

Функція чекатиме, доки майбутнє фактично не буде скасовано, тому загальний час очікування може перевищити тайм-аут. Якщо під час скасування виникає виняток, він поширюється.

Якщо очікування скасовується, майбутнє aw також скасовується.

Приклад:

async def eternity():
    # Sleep for one hour
    await asyncio.sleep(3600)
    print('yay!')

async def main():
    # Wait for at most 1 second
    try:
        await asyncio.wait_for(eternity(), timeout=1.0)
    except TimeoutError:
        print('timeout!')

asyncio.run(main())

# Expected output:
#
#     timeout!

Змінено в версії 3.7: When aw is cancelled due to a timeout, wait_for waits for aw to be cancelled. Previously, it raised TimeoutError immediately.

Змінено в версії 3.10: Видалено параметр loop.

Змінено в версії 3.11: Raises TimeoutError instead of asyncio.TimeoutError.

Очікування примітивів

coroutine asyncio.wait(aws, *, timeout=None, return_when=ALL_COMPLETED)

Run Future and Task instances in the aws iterable concurrently and block until the condition specified by return_when.

The aws iterable must not be empty and generators yielding tasks are not accepted.

Повертає два набори Tasks/Futures: (done, pending).

Використання:

done, pending = await asyncio.wait(aws)

timeout (float або int), якщо вказано, можна використовувати для керування максимальною кількістю секунд очікування перед поверненням.

Note that this function does not raise TimeoutError. Futures or Tasks that aren’t done when the timeout occurs are simply returned in the second set.

return_when вказує, коли ця функція має повернутися. Це має бути одна з таких констант:

Постійний

опис

asyncio.FIRST_COMPLETED

Функція повернеться, коли будь-який майбутній завершиться або буде скасовано.

asyncio.FIRST_EXCEPTION

The function will return when any future finishes by raising an exception. If no future raises an exception then it is equivalent to ALL_COMPLETED.

asyncio.ALL_COMPLETED

Функція повернеться, коли всі ф’ючерси закінчаться або будуть скасовані.

На відміну від wait_for(), wait() не скасовує ф’ючерси, коли настає тайм-аут.

Змінено в версії 3.10: Видалено параметр loop.

Змінено в версії 3.11: Passing coroutine objects to wait() directly is forbidden.

asyncio.as_completed(aws, *, timeout=None)

Run awaitable objects in the aws iterable concurrently. Generators yielding tasks are not accepted as aws iterable. Return an iterator of coroutines. Each coroutine returned can be awaited to get the earliest next result from the iterable of the remaining awaitables.

Raises TimeoutError if the timeout occurs before all Futures are done.

Приклад:

for coro in as_completed(aws):
    earliest_result = await coro
    # ...

Змінено в версії 3.10: Видалено параметр loop.

Застаріло починаючи з версії 3.10: Якщо не всі очікувані об’єкти в aws iterable є об’єктами, подібними до майбутнього, і немає запущеного циклу подій, видається попередження про застаріле.

Запуск у потоках

coroutine asyncio.to_thread(func, /, *args, **kwargs)

Асинхронний запуск функції func в окремому потоці.

Будь-які *args і **kwargs, надані для цієї функції, безпосередньо передаються до func. Крім того, поточний contextvars.Context поширюється, дозволяючи доступ до змінних контексту з потоку циклу подій в окремому потоці.

Повертає співпрограму, яку можна очікувати, щоб отримати кінцевий результат func.

This coroutine function is primarily intended to be used for executing IO-bound functions/methods that would otherwise block the event loop if they were run in the main thread. For example:

def blocking_io():
    print(f"start blocking_io at {time.strftime('%X')}")
    # Note that time.sleep() can be replaced with any blocking
    # IO-bound operation, such as file operations.
    time.sleep(1)
    print(f"blocking_io complete at {time.strftime('%X')}")

async def main():
    print(f"started main at {time.strftime('%X')}")

    await asyncio.gather(
        asyncio.to_thread(blocking_io),
        asyncio.sleep(1))

    print(f"finished main at {time.strftime('%X')}")


asyncio.run(main())

# Expected output:
#
# started main at 19:50:53
# start blocking_io at 19:50:53
# blocking_io complete at 19:50:54
# finished main at 19:50:54

Directly calling blocking_io() in any coroutine would block the event loop for its duration, resulting in an additional 1 second of run time. Instead, by using asyncio.to_thread(), we can run it in a separate thread without blocking the event loop.

Примітка

Due to the GIL, asyncio.to_thread() can typically only be used to make IO-bound functions non-blocking. However, for extension modules that release the GIL or alternative Python implementations that don’t have one, asyncio.to_thread() can also be used for CPU-bound functions.

Нове в версії 3.9.

Планування з інших потоків

asyncio.run_coroutine_threadsafe(coro, loop)

Надішліть співпрограму в заданий цикл подій. Ниткобезпечний.

Поверніть concurrent.futures.Future, щоб дочекатися результату від іншого потоку ОС.

Цю функцію призначено для виклику з потоку ОС, відмінного від того, у якому виконується цикл подій. Приклад:

# Create a coroutine
coro = asyncio.sleep(1, result=3)

# Submit the coroutine to a given loop
future = asyncio.run_coroutine_threadsafe(coro, loop)

# Wait for the result with an optional timeout argument
assert future.result(timeout) == 3

Якщо в співпрограмі виникає виняток, буде повідомлено про повернутий Future. Його також можна використовувати для скасування завдання в циклі подій:

try:
    result = future.result(timeout)
except TimeoutError:
    print('The coroutine took too long, cancelling the task...')
    future.cancel()
except Exception as exc:
    print(f'The coroutine raised an exception: {exc!r}')
else:
    print(f'The coroutine returned: {result!r}')

Перегляньте розділ паралелізм і багатопотоковість документації.

На відміну від інших асинхронних функцій, ця функція вимагає явної передачі аргументу loop.

Нове в версії 3.5.1.

самоаналіз

asyncio.current_task(loop=None)

Повертає поточний екземпляр Task або None, якщо жодне завдання не виконується.

Якщо loop має значення None get_running_loop() використовується для отримання поточного циклу.

Нове в версії 3.7.

asyncio.all_tasks(loop=None)

Повертає набір ще не завершених об’єктів Task, які виконуються циклом.

Якщо loop має значення None, get_running_loop() використовується для отримання поточного циклу.

Нове в версії 3.7.

asyncio.iscoroutine(obj)

Return True if obj is a coroutine object.

Нове в версії 3.4.

Об’єкт завдання

class asyncio.Task(coro, *, loop=None, name=None, context=None)

Об’єкт подібний до майбутнього, який запускає сопрограму Python. Небезпечно для потоків.

Завдання використовуються для виконання співпрограм у циклах подій. Якщо співпрограма очікує на Future, Завдання призупиняє виконання співпрограми та чекає завершення Future. Коли Future done, виконання загорнутої співпрограми відновлюється.

Цикли подій використовують кооперативне планування: цикл подій виконує одне завдання за раз. Поки Завдання очікує завершення Майбутнього, цикл подій запускає інші Завдання, зворотні виклики або виконує операції введення-виведення.

Використовуйте функцію високого рівня asyncio.create_task() для створення завдань або функції низького рівня loop.create_task() або ensure_future(). Не рекомендується створювати завдання вручну.

Щоб скасувати запущене завдання, використовуйте метод cancel(). Його виклик призведе до того, що завдання створить виняток CancelledError у загорнутій співпрограмі. Якщо співпрограма очікує на об’єкті Future під час скасування, об’єкт Future буде скасовано.

cancelled() можна використовувати, щоб перевірити, чи було скасовано завдання. Метод повертає True, якщо загорнута співпрограма не придушила виняток CancelledError і була фактично скасована.

asyncio.Task успадковує від Future усі його API, крім Future.set_result() і Future.set_exception().

An optional keyword-only context argument allows specifying a custom contextvars.Context for the coro to run in. If no context is provided, the Task copies the current context and later runs its coroutine in the copied context.

Змінено в версії 3.7: Додано підтримку модуля contextvars.

Змінено в версії 3.8: Додано параметр name.

Застаріло починаючи з версії 3.10: Якщо loop не вказано і немає запущеного циклу подій, видається попередження про застаріле.

Змінено в версії 3.11: Added the context parameter.

done()

Повертає True, якщо завдання виконано.

Завдання вважається виконаним, коли загорнута співпрограма або повернула значення, викликала виняток, або завдання було скасовано.

result()

Повернути результат Завдання.

Якщо завдання виконано, повертається результат загорнутої співпрограми (або якщо співпрограма викликала виняток, цей виняток викликається повторно).

Якщо завдання було скасовано, цей метод викликає виняток CancelledError.

Якщо результат завдання ще не доступний, цей метод викликає виняток InvalidStateError.

exception()

Повернути виняток Завдання.

Якщо загорнута співпрограма викликала виняток, цей виняток повертається. Якщо загорнута співпрограма повертає нормальний результат, цей метод повертає None.

Якщо завдання було скасовано, цей метод викликає виняток CancelledError.

Якщо завдання ще не виконано, цей метод викликає виняток InvalidStateError.

add_done_callback(callback, *, context=None)

Додайте зворотний виклик, який буде запущено, коли Завдання виконано.

Цей метод слід використовувати лише в низькорівневому коді на основі зворотного виклику.

Перегляньте документацію Future.add_done_callback() для отримання додаткової інформації.

remove_done_callback(callback)

Видалити callback зі списку зворотних викликів.

Цей метод слід використовувати лише в низькорівневому коді на основі зворотного виклику.

Перегляньте документацію Future.remove_done_callback() для отримання додаткової інформації.

get_stack(*, limit=None)

Повернути список фреймів стека для цього завдання.

Якщо загорнуту співпрограму не виконано, це повертає стек, де він був призупинений. Якщо співпрограма завершилася успішно або була скасована, повертається порожній список. Якщо співпрограму було припинено через виняток, повертається список кадрів трасування.

Рамки завжди впорядковуються від найстаріших до найновіших.

Для призупиненої співпрограми повертається лише один кадр стека.

Необов’язковий аргумент limit встановлює максимальну кількість кадрів для повернення; за замовчуванням повертаються всі доступні кадри. Порядок поверненого списку відрізняється залежно від того, повертається стек чи трасування: повертаються найновіші кадри стеку, але повертаються найстаріші кадри трасування. (Це відповідає поведінці модуля відстеження.)

print_stack(*, limit=None, file=None)

Роздрукуйте стек або відстеження для цього завдання.

Це створює вихідні дані, подібні до результатів модуля трасування для кадрів, отриманих get_stack().

Аргумент limit передається безпосередньо в get_stack().

The file argument is an I/O stream to which the output is written; by default output is written to sys.stdout.

get_coro()

Повертає об’єкт співпрограми, обгорнутий Task.

Нове в версії 3.8.

get_name()

Повернути назву завдання.

Якщо Завданню не було явно призначено ім’я, реалізація асинхронного Завдання за замовчуванням генерує ім’я за замовчуванням під час створення екземпляра.

Нове в версії 3.8.

set_name(value)

Встановіть назву завдання.

Аргументом value може бути будь-який об’єкт, який потім перетворюється на рядок.

У реалізації Task за замовчуванням ім’я буде видно у виводі repr() об’єкта task.

Нове в версії 3.8.

cancel(msg=None)

Вимагайте скасування Завдання.

Це організовує виняток CancelledError, який буде створено в загорнутій співпрограмі в наступному циклі циклу подій.

The coroutine then has a chance to clean up or even deny the request by suppressing the exception with a try … … except CancelledErrorfinally block. Therefore, unlike Future.cancel(), Task.cancel() does not guarantee that the Task will be cancelled, although suppressing cancellation completely is not common and is actively discouraged. Should the coroutine nevertheless decide to suppress the cancellation, it needs to call Task.uncancel() in addition to catching the exception.

Змінено в версії 3.9: Додано параметр msg.

Змінено в версії 3.11: The msg parameter is propagated from cancelled task to its awaiter.

Наступний приклад ілюструє, як співпрограми можуть перехопити запит на скасування:

async def cancel_me():
    print('cancel_me(): before sleep')

    try:
        # Wait for 1 hour
        await asyncio.sleep(3600)
    except asyncio.CancelledError:
        print('cancel_me(): cancel sleep')
        raise
    finally:
        print('cancel_me(): after sleep')

async def main():
    # Create a "cancel_me" Task
    task = asyncio.create_task(cancel_me())

    # Wait for 1 second
    await asyncio.sleep(1)

    task.cancel()
    try:
        await task
    except asyncio.CancelledError:
        print("main(): cancel_me is cancelled now")

asyncio.run(main())

# Expected output:
#
#     cancel_me(): before sleep
#     cancel_me(): cancel sleep
#     cancel_me(): after sleep
#     main(): cancel_me is cancelled now
cancelled()

Повертає True, якщо завдання скасовано.

Завдання скасовується, коли запит на скасування надійшов за допомогою cancel(), а загорнута співпрограма поширила виняткову ситуацію CancelledError, яка виникла в ній.

uncancel()

Decrement the count of cancellation requests to this Task.

Returns the remaining number of cancellation requests.

Note that once execution of a cancelled task completed, further calls to uncancel() are ineffective.

Нове в версії 3.11.

This method is used by asyncio’s internals and isn’t expected to be used by end-user code. In particular, if a Task gets successfully uncancelled, this allows for elements of structured concurrency like Task Groups and asyncio.timeout() to continue running, isolating cancellation to the respective structured block. For example:

async def make_request_with_timeout():
    try:
        async with asyncio.timeout(1):
            # Structured block affected by the timeout:
            await make_request()
            await make_another_request()
    except TimeoutError:
        log("There was a timeout")
    # Outer code not affected by the timeout:
    await unrelated_code()

While the block with make_request() and make_another_request() might get cancelled due to the timeout, unrelated_code() should continue running even in case of the timeout. This is implemented with uncancel(). TaskGroup context managers use uncancel() in a similar fashion.

If end-user code is, for some reason, suppresing cancellation by catching CancelledError, it needs to call this method to remove the cancellation state.

cancelling()

Return the number of pending cancellation requests to this Task, i.e., the number of calls to cancel() less the number of uncancel() calls.

Note that if this number is greater than zero but the Task is still executing, cancelled() will still return False. This is because this number can be lowered by calling uncancel(), which can lead to the task not being cancelled after all if the cancellation requests go down to zero.

This method is used by asyncio’s internals and isn’t expected to be used by end-user code. See uncancel() for more details.

Нове в версії 3.11.