Skip to content

gh-128307: support eager_start kwarg in create_eager_task_factory #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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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=...)

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
4 changes: 2 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,9 +1030,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
18 changes: 18 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
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