Skip to content

gh-128307: support eager_start kwarg in create_eager_task_factory, and pass kwargs from asyncio.create_task and TaskGroup.create_task #128306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
23d2c42
support eager_start kwarg in create_eager_task_factory
graingert Dec 28, 2024
da5cab3
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 28, 2024
7bce401
Update Misc/NEWS.d/next/Library/2024-12-28-11-01-36.gh-issue-128307.B…
graingert Dec 28, 2024
8ad26fc
Update Lib/asyncio/base_events.py
graingert Jan 21, 2025
90f9ef2
Update Lib/asyncio/tasks.py
graingert Jan 21, 2025
1107608
use presense rather than None-ness to determine default eager_start b…
graingert Jan 21, 2025
ec3bd14
Update Doc/library/asyncio-eventloop.rst
graingert Jan 21, 2025
939c3f5
Merge branch 'main' into add-eager-start-to-create-task
graingert Jan 21, 2025
d24d43b
Update Misc/NEWS.d/next/Library/2024-12-28-11-01-36.gh-issue-128307.B…
graingert Jan 21, 2025
d8369c3
Merge branch 'main' into add-eager-start-to-create-task
graingert Jan 26, 2025
4a4ad1b
Apply suggestions from code review
graingert Jan 26, 2025
813398c
Merge branch 'main' into add-eager-start-to-create-task
graingert Mar 25, 2025
dfafc7a
Merge branch 'main' into add-eager-start-to-create-task
graingert Mar 25, 2025
3175aae
use self.current_task
graingert Mar 25, 2025
0598877
Merge branch 'main' into add-eager-start-to-create-task
graingert Apr 10, 2025
3e8e2a8
Merge branch 'main' into add-eager-start-to-create-task
graingert Apr 19, 2025
1c10063
Merge branch 'main' into add-eager-start-to-create-task
graingert Apr 22, 2025
5288bf2
Merge branch 'main' into add-eager-start-to-create-task
graingert Apr 29, 2025
14e0536
Update Doc/library/asyncio-eventloop.rst
graingert May 4, 2025
73a20a5
Merge branch 'main' into add-eager-start-to-create-task
graingert May 4, 2025
c92e2fa
test eager start with default factory
graingert May 4, 2025
8aab5f1
Merge branch 'main' into add-eager-start-to-create-task
graingert May 5, 2025
a410125
Merge branch 'main' into add-eager-start-to-create-task
graingert May 5, 2025
1a45d43
Change TaskGroup.create_task and tasks.create_task to **kwargs
gvanrossum-ms May 5, 2025
7654a85
Fix bug I introduced in tasks.create_task()
gvanrossum-ms May 5, 2025
74b02d4
Update Lib/asyncio/tasks.py
graingert May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Doc/library/asyncio-eventloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ Creating Futures and Tasks

.. versionadded:: 3.5.2

.. method:: loop.create_task(coro, *, name=None, context=None)
.. method:: loop.create_task(coro, *, name=None, context=None, eager_start=None)

Schedule the execution of :ref:`coroutine <coroutine>` *coro*.
Return a :class:`Task` object.
Expand All @@ -377,12 +377,20 @@ Creating Futures and Tasks
custom :class:`contextvars.Context` for the *coro* to run in.
The current context copy is created when no *context* is provided.

An optional keyword-only *eager_start* argument allows specifying
if the task should execute eagerly during the call to create_task,
or be scheduled later. If *eager_start* is not passed the mode set
by :meth:`loop.set_task_factory` will be used.

.. versionchanged:: 3.8
Added the *name* parameter.

.. versionchanged:: 3.11
Added the *context* parameter.

.. versionchanged:: next
Added the *eager_start* parameter.

.. method:: loop.set_task_factory(factory)

Set a task factory that will be used by
Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def create_future(self):
return futures.Future(loop=self)

def create_task(self, coro, **kwargs):
"""Schedule a coroutine object.
"""Schedule or begin executing a coroutine object.

Return a task object.
"""
Expand Down
7 changes: 2 additions & 5 deletions Lib/asyncio/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async def _aexit(self, et, exc):
exc = None


def create_task(self, coro, *, name=None, context=None):
def create_task(self, coro, **kwargs):
"""Create a new task in this group and return it.

Similar to `asyncio.create_task`.
Expand All @@ -193,10 +193,7 @@ def create_task(self, coro, *, name=None, context=None):
if self._aborting:
coro.close()
raise RuntimeError(f"TaskGroup {self!r} is shutting down")
if context is None:
task = self._loop.create_task(coro, name=name)
else:
task = self._loop.create_task(coro, name=name, context=context)
task = self._loop.create_task(coro, **kwargs)

futures.future_add_to_awaited_by(task, self._parent_task)

Expand Down
14 changes: 4 additions & 10 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,19 +386,13 @@ def __wakeup(self, future):
Task = _CTask = _asyncio.Task


def create_task(coro, *, name=None, context=None):
def create_task(coro, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Python user perspective, I don't like this change.
It obscures the allowed kwargs and makes it harder for IDEs to provide suggestions.

I suppose we'd be relying on https://github.com/python/typeshed/blob/main/stdlib/asyncio/tasks.pyi from now on?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it would be nice if we could avoid the **kwargs, but it make the implementation more complex at every level of indirection (asyncio.create_task -> loop.create_task -> Task, and also TaskGroup.create_task -> loop.create_task -> Task).

IDEs should be using typeshed anyway. (And we should update tasks.pyi to have a case for 3.14 that adds eager_start and **kwargs. In fact this should be done to all the create_task definitions found in typeshed's asyncio definitions.

"""Schedule the execution of a coroutine object in a spawn task.

Return a Task object.
"""
loop = events.get_running_loop()
if context is None:
# Use legacy API if context is not needed
task = loop.create_task(coro, name=name)
else:
task = loop.create_task(coro, name=name, context=context)

return task
return loop.create_task(coro, **kwargs)


# wait() and as_completed() similar to those in PEP 3148.
Expand Down Expand Up @@ -1030,9 +1024,9 @@ def create_eager_task_factory(custom_task_constructor):
used. E.g. `loop.set_task_factory(asyncio.eager_task_factory)`.
"""

def factory(loop, coro, *, name=None, context=None):
def factory(loop, coro, *, eager_start=True, **kwargs):
return custom_task_constructor(
coro, loop=loop, name=name, context=context, eager_start=True)
coro, loop=loop, eager_start=eager_start, **kwargs)

return factory

Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_asyncio/test_eager_task_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ async def run():

self.run_coro(run())

def test_eager_start_false(self):
name = None

async def asyncfn():
nonlocal name
name = asyncio.current_task().get_name()

async def main():
t = asyncio.get_running_loop().create_task(
asyncfn(), eager_start=False, name="example"
)
self.assertFalse(t.done())
self.assertIsNone(name)
await t
self.assertEqual(name, "example")

self.run_coro(main())


class PyEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase):
Task = tasks._PyTask
Expand Down Expand Up @@ -505,5 +523,24 @@ def tearDown(self):
asyncio.current_task = asyncio.tasks.current_task = self._current_task
return super().tearDown()


class DefaultTaskFactoryEagerStart(test_utils.TestCase):
def test_eager_start_true_with_default_factory(self):
name = None

async def asyncfn():
nonlocal name
name = asyncio.current_task().get_name()

async def main():
t = asyncio.get_running_loop().create_task(
asyncfn(), eager_start=True, name="example"
)
self.assertTrue(t.done())
self.assertEqual(name, "example")
await t

asyncio.run(main(), loop_factory=asyncio.EventLoop)

if __name__ == '__main__':
unittest.main()
33 changes: 31 additions & 2 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class BaseTaskTests:
Future = None
all_tasks = None

def new_task(self, loop, coro, name='TestTask', context=None):
return self.__class__.Task(coro, loop=loop, name=name, context=context)
def new_task(self, loop, coro, name='TestTask', context=None, eager_start=None):
return self.__class__.Task(coro, loop=loop, name=name, context=context, eager_start=eager_start)

def new_future(self, loop):
return self.__class__.Future(loop=loop)
Expand Down Expand Up @@ -2686,6 +2686,35 @@ async def main():

self.assertEqual([None, 1, 2], ret)

def test_eager_start_true(self):
name = None

async def asyncfn():
nonlocal name
name = self.current_task().get_name()

async def main():
t = self.new_task(coro=asyncfn(), loop=asyncio.get_running_loop(), eager_start=True, name="example")
self.assertTrue(t.done())
self.assertEqual(name, "example")
await t

def test_eager_start_false(self):
name = None

async def asyncfn():
nonlocal name
name = self.current_task().get_name()

async def main():
t = self.new_task(coro=asyncfn(), loop=asyncio.get_running_loop(), eager_start=False, name="example")
self.assertFalse(t.done())
self.assertIsNone(name)
await t
self.assertEqual(name, "example")

asyncio.run(main(), loop_factory=asyncio.EventLoop)

def test_get_coro(self):
loop = asyncio.new_event_loop()
coro = coroutine_function()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``eager_start`` keyword argument to :meth:`asyncio.loop.create_task`
Loading