Skip to content

Commit aa0952b

Browse files
Pass name to BaseEventLoop task factory
The task factory is used in the BaseEventLoop create_task method when set. This commit passes the `name` kwarg to this factory to set the name of the task, instead of setting the task in the loop method. This is more consistent with the eager_task_factory methods.
1 parent 3531ea4 commit aa0952b

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ Creating Futures and Tasks
379379

380380
If *factory* is ``None`` the default task factory will be set.
381381
Otherwise, *factory* must be a *callable* with the signature matching
382-
``(loop, coro, context=None)``, where *loop* is a reference to the active
383-
event loop, and *coro* is a coroutine object. The callable
382+
``(loop, coro, name=None, context=None)``, where *loop* is a reference
383+
to the active event loop, and *coro* is a coroutine object. The callable
384384
must return a :class:`asyncio.Future`-compatible object.
385385

386386
.. method:: loop.get_task_factory()

Lib/asyncio/base_events.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,9 @@ def create_task(self, coro, *, name=None, context=None):
461461
else:
462462
if context is None:
463463
# Use legacy API if context is not needed
464-
task = self._task_factory(self, coro)
464+
task = self._task_factory(self, coro, name=name)
465465
else:
466-
task = self._task_factory(self, coro, context=context)
467-
468-
task.set_name(name)
466+
task = self._task_factory(self, coro, name=name, context=context)
469467

470468
return task
471469

Lib/test/test_asyncio/test_base_events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ class MyTask(asyncio.Task):
722722
async def coro():
723723
pass
724724

725-
factory = lambda loop, coro: MyTask(coro, loop=loop)
725+
factory = lambda loop, coro, **kwargs: MyTask(coro, loop=loop, **kwargs)
726726

727727
self.assertIsNone(self.loop.get_task_factory())
728728
self.loop.set_task_factory(factory)
@@ -816,8 +816,8 @@ async def test():
816816
loop.close()
817817

818818
def test_create_named_task_with_custom_factory(self):
819-
def task_factory(loop, coro):
820-
return asyncio.Task(coro, loop=loop)
819+
def task_factory(loop, coro, **kwargs):
820+
return asyncio.Task(coro, loop=loop, **kwargs)
821821

822822
async def test():
823823
pass

Lib/test/test_asyncio/test_tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3326,7 +3326,7 @@ def test_run_coroutine_threadsafe_task_factory_exception(self):
33263326
"""Test coroutine submission from a thread to an event loop
33273327
when the task factory raise an exception."""
33283328

3329-
def task_factory(loop, coro):
3329+
def task_factory(loop, coro, **kwargs):
33303330
raise NameError
33313331

33323332
run = self.loop.run_in_executor(

0 commit comments

Comments
 (0)