Skip to content

Commit 7b0543e

Browse files
asvetlovgraingertkumaraditya303
authored
[3.13] gh-128308: pass **kwargs to asyncio task_factory (GH-128768) (#130084)
* [3.13] gh-128308: pass `**kwargs` to asyncio task_factory (GH-128768) (cherry picked from commit 38a9956) Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Kumar Aditya <[email protected]> --------- Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
1 parent 9e23e0a commit 7b0543e

File tree

7 files changed

+40
-21
lines changed

7 files changed

+40
-21
lines changed

Doc/library/asyncio-eventloop.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@ Creating Futures and Tasks
382382

383383
If *factory* is ``None`` the default task factory will be set.
384384
Otherwise, *factory* must be a *callable* with the signature matching
385-
``(loop, coro, context=None)``, where *loop* is a reference to the active
385+
``(loop, coro, **kwargs)``, where *loop* is a reference to the active
386386
event loop, and *coro* is a coroutine object. The callable
387-
must return a :class:`asyncio.Future`-compatible object.
387+
must pass on all *kwargs*, and return a :class:`asyncio.Task`-compatible object.
388388

389389
.. method:: loop.get_task_factory()
390390

Lib/asyncio/base_events.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -458,25 +458,18 @@ def create_future(self):
458458
"""Create a Future object attached to the loop."""
459459
return futures.Future(loop=self)
460460

461-
def create_task(self, coro, *, name=None, context=None):
461+
def create_task(self, coro, **kwargs):
462462
"""Schedule a coroutine object.
463463
464464
Return a task object.
465465
"""
466466
self._check_closed()
467-
if self._task_factory is None:
468-
task = tasks.Task(coro, loop=self, name=name, context=context)
469-
if task._source_traceback:
470-
del task._source_traceback[-1]
471-
else:
472-
if context is None:
473-
# Use legacy API if context is not needed
474-
task = self._task_factory(self, coro)
475-
else:
476-
task = self._task_factory(self, coro, context=context)
477-
478-
task.set_name(name)
467+
if self._task_factory is not None:
468+
return self._task_factory(self, coro, **kwargs)
479469

470+
task = tasks.Task(coro, loop=self, **kwargs)
471+
if task._source_traceback:
472+
del task._source_traceback[-1]
480473
try:
481474
return task
482475
finally:
@@ -490,9 +483,10 @@ def set_task_factory(self, factory):
490483
If factory is None the default task factory will be set.
491484
492485
If factory is a callable, it should have a signature matching
493-
'(loop, coro)', where 'loop' will be a reference to the active
494-
event loop, 'coro' will be a coroutine object. The callable
495-
must return a Future.
486+
'(loop, coro, **kwargs)', where 'loop' will be a reference to the active
487+
event loop, 'coro' will be a coroutine object, and **kwargs will be
488+
arbitrary keyword arguments that should be passed on to Task.
489+
The callable must return a Task.
496490
"""
497491
if factory is not None and not callable(factory):
498492
raise TypeError('task factory must be a callable or None')

Lib/asyncio/events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def create_future(self):
292292

293293
# Method scheduling a coroutine object: create a task.
294294

295-
def create_task(self, coro, *, name=None, context=None):
295+
def create_task(self, coro, **kwargs):
296296
raise NotImplementedError
297297

298298
# Methods for interacting with threads.

Lib/test/test_asyncio/test_base_events.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,8 @@ async def test():
833833
loop.close()
834834

835835
def test_create_named_task_with_custom_factory(self):
836-
def task_factory(loop, coro):
837-
return asyncio.Task(coro, loop=loop)
836+
def task_factory(loop, coro, **kwargs):
837+
return asyncio.Task(coro, loop=loop, **kwargs)
838838

839839
async def test():
840840
pass

Lib/test/test_asyncio/test_eager_task_factory.py

+12
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,18 @@ async def run():
302302

303303
self.run_coro(run())
304304

305+
def test_name(self):
306+
name = None
307+
async def coro():
308+
nonlocal name
309+
name = asyncio.current_task().get_name()
310+
311+
async def main():
312+
task = self.loop.create_task(coro(), name="test name")
313+
self.assertEqual(name, "test name")
314+
await task
315+
316+
self.run_coro(coro())
305317

306318
class AsyncTaskCounter:
307319
def __init__(self, loop, *, task_class, eager):

Lib/test/test_asyncio/test_taskgroups.py

+12
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,18 @@ async def throw_error():
10811081
# cancellation happens here and error is more understandable
10821082
await asyncio.sleep(0)
10831083

1084+
async def test_name(self):
1085+
name = None
1086+
1087+
async def asyncfn():
1088+
nonlocal name
1089+
name = asyncio.current_task().get_name()
1090+
1091+
async with asyncio.TaskGroup() as tg:
1092+
tg.create_task(asyncfn(), name="example name")
1093+
1094+
self.assertEqual(name, "example name")
1095+
10841096

10851097
class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
10861098
loop_factory = asyncio.EventLoop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support the *name* keyword argument for eager tasks in :func:`asyncio.loop.create_task`, :func:`asyncio.create_task` and :func:`asyncio.TaskGroup.create_task`, by passing on all *kwargs* to the task factory set by :func:`asyncio.loop.set_task_factory`.

0 commit comments

Comments
 (0)