GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-128307: support eager_start kwarg in create_eager_task_factory, and pass kwargs from asyncio.create_task and TaskGroup.create_task by graingert · Pull Request #128306 · python/cpython

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.

Sign up for GitHub

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
gvanrossum merged 26 commits into python:main from graingert:add-eager-start-to-create-task
May 5, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

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

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

10 changes: 9 additions & 1 deletion 10 Doc/library/asyncio-eventloop.rst
Show comments
View file
Open in desktop
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)
Comment thread
graingert marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


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 2 Lib/asyncio/base_events.py
Show comments
View file
Open in desktop
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.
Comment thread
graingert marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


Return a task object.
"""
Expand Down
7 changes: 2 additions & 5 deletions 7 Lib/asyncio/taskgroups.py
Show comments
View file
Open in desktop
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 14 Lib/asyncio/tasks.py
Show comments
View file
Open in desktop
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):
Comment thread

@dimaqq dimaqq May 6, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gvanrossum gvanrossum May 6, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
"""Schedule the execution of a coroutine object in a spawn task.
Comment thread
graingert marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


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 37 Lib/test/test_asyncio/test_eager_task_factory.py
Show comments
View file
Open in desktop
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())
Comment thread
graingert marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

self.assertEqual(name, "example")
await t

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

if __name__ == '__main__':
unittest.main()
33 changes: 31 additions & 2 deletions 33 Lib/test/test_asyncio/test_tasks.py
Show comments
View file
Open in desktop
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
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2024-12-28-11-01-36.gh-issue-128307.BRCYTA.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``eager_start`` keyword argument to :meth:`asyncio.loop.create_task`
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.