CKTIS / Python
asyncio.create_task
Schedules a coroutine as a task in the running event loop and returns a handle to its result.
Version: Python 3.14; repository image, production runtime not attested
Syntax
asyncio.create_task(coro, *, name=None, context=None, eager_start=None, **kwargs)Schedules a coroutine as a task in the running event loop and returns a handle to its result.
Retaining and awaiting the handle ensures that the caller observes the task’s result and any exception.
Schedule a coroutine on the running loop and return a Task. By default its context is copied from the caller. Keep a strong reference and await the handle to collect its result or exception. In 3.14 eager_start=True allows immediate execution until the first suspension. Without a running loop create_task raises RuntimeError. The example returns 8 and done() is True. In 3.14 additional **kwargs reach loop.create_task. When eager_start is omitted, the loop task factory determines the starting policy.
import asyncio
async def main():
steps = []
async def tick():
steps.append("start")
await asyncio.sleep(0)
return 8
t = asyncio.create_task(tick(), name="tick", eager_start=True)
assert steps == ["start"] and t.get_name() == "tick"
assert await t == 8 and t.done()
asyncio.run(main())
import asyncio
async def main():
loop = asyncio.get_running_loop()
received = []
def factory(loop, coro, **opts):
received.append(opts.pop("marker", None))
return asyncio.Task(coro, loop=loop, **opts)
loop.set_task_factory(factory)
try:
assert await asyncio.create_task(asyncio.sleep(0, result=3), marker=7) == 3
finally:
loop.set_task_factory(None)
assert received == [7]
asyncio.run(main())
Examples
import asyncio
async def main():
task = asyncio.create_task(asyncio.sleep(0, result=8))
print(await task)
print(task.done())
asyncio.run(main())
Result
8 True
Schedule a coroutine on the running loop and return a Task. By default its context is copied from the caller. Keep a strong reference and await the handle to collect its result or exception. In 3.14 eager_start=True allows immediate execution until the first suspension. Without a running loop create_task raises RuntimeError. The example returns 8 and done() is True. In 3.14 additional **kwargs reach loop.create_task. When eager_start is omitted, the loop task factory determines the starting policy.