@@ -50,6 +50,7 @@ include "errors.pyx"
50
50
51
51
cdef:
52
52
int PY39 = PY_VERSION_HEX >= 0x03090000
53
+ int PY311 = PY_VERSION_HEX >= 0x030b0000
53
54
uint64_t MAX_SLEEP = 3600 * 24 * 365 * 100
54
55
55
56
@@ -1413,19 +1414,31 @@ cdef class Loop:
1413
1414
""" Create a Future object attached to the loop."""
1414
1415
return self ._new_future()
1415
1416
1416
- def create_task (self , coro , *, name = None ):
1417
+ def create_task (self , coro , *, name = None , context = None ):
1417
1418
""" Schedule a coroutine object.
1418
1419
1419
1420
Return a task object.
1420
1421
1421
1422
If name is not None, task.set_name(name) will be called if the task
1422
1423
object has the set_name attribute, true for default Task in Python 3.8.
1424
+
1425
+ An optional keyword-only context argument allows specifying a custom
1426
+ contextvars.Context for the coro to run in. The current context copy is
1427
+ created when no context is provided. Only available in Python 3.11.
1423
1428
"""
1424
1429
self ._check_closed()
1425
- if self ._task_factory is None :
1426
- task = aio_Task(coro, loop = self )
1430
+ if PY311:
1431
+ if self ._task_factory is None :
1432
+ task = aio_Task(coro, loop = self , context = context)
1433
+ else :
1434
+ task = self ._task_factory(self , coro, context = context)
1427
1435
else :
1428
- task = self ._task_factory(self , coro)
1436
+ if UVLOOP_DEBUG:
1437
+ assert context is None
1438
+ if self ._task_factory is None :
1439
+ task = aio_Task(coro, loop = self )
1440
+ else :
1441
+ task = self ._task_factory(self , coro)
1429
1442
1430
1443
# copied from asyncio.tasks._set_task_name (bpo-34270)
1431
1444
if name is not None :
0 commit comments