Python 3.7+Async programmingfunction
asyncio.create_task()
tasks
Wraps a coroutine in a Task and schedules it on the event loop: it starts at the current coroutine's next await, and you need not await its result right away.
Syntax
task = asyncio.create_task(coro())
Nothing runs at the call site itself: the task is queued on the event loop and only starts once the current coroutine reaches its next await. Also keep a reference — the loop holds only a weak one, so a task assigned nowhere can be garbage-collected mid-flight; park it in a variable or a set until it finishes. An exception raised inside a task stays silent until someone awaits it, which is how fire-and-forget tasks swallow bugs.
Examples
import asyncio
async def bg():
await asyncio.sleep(0)
return 'done'
async def main():
t = asyncio.create_task(bg())
print('working...') # → working... — задача запланирована, но ещё не начала выполняться
result = await t
print(result) # → done
asyncio.run(main())
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy