On this page
asyncio.create_task
Prev: asyncio.create_subprocess_shell | Up: asyncio | Next: asyncio.current_task
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.create_task
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import create_task
Signature & Overview
asyncio.create_task(coro, **kwargs)
Schedule the execution of a coroutine object in a spawn task.
Usage
import asyncio
async def double(x: int) -> int:
await asyncio.sleep(0)
return x * 2
async def main():
task = asyncio.create_task(double(21), name="double-21")
assert await task == 42
asyncio.run(main())
Gotchas
- Keep a strong reference to fire-and-forget tasks: The event loop holds only weak references, so an unreferenced task can be garbage-collected mid-flight—store background tasks in a
setand discard viaadd_done_callback(tasks.discard).
REPL Inspect
from asyncio import create_task
help(create_task)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/create_subprocess_shell Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/current_task Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/Barrier shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseProtocol shared tags: #asyncio, #stdlib