Skip to content

Commit

Permalink
add eager_start to create_task
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Dec 28, 2024
1 parent aeb9b65 commit c809133
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
14 changes: 8 additions & 6 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,24 +458,26 @@ def create_future(self):
"""Create a Future object attached to the loop."""
return futures.Future(loop=self)

def create_task(self, coro, *, name=None, context=None):
def create_task(self, coro, *, name=None, context=None, eager=None):
"""Schedule a coroutine object.
Return a task object.
"""
self._check_closed()
if self._task_factory is None:
task = tasks.Task(coro, loop=self, name=name, context=context)
task = tasks.Task(coro, loop=self, name=name, context=context, eager_start=eager_start)
if task._source_traceback:
del task._source_traceback[-1]
else:
if context is None:
if context is None and eager_start is None:
# Use legacy API if context is not needed
task = self._task_factory(self, coro)
else:
task.set_name(name)
elif eager_start is None:
task = self._task_factory(self, coro, context=context)

task.set_name(name)
task.set_name(name)
else:
task = self._task_factory(self, coro, context=context, eager=eager_start, name=name)

return task

Expand Down
8 changes: 5 additions & 3 deletions Lib/asyncio/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async def _aexit(self, et, exc):
exc = None


def create_task(self, coro, *, name=None, context=None):
def create_task(self, coro, *, name=None, context=None, eager_start=None):
"""Create a new task in this group and return it.
Similar to `asyncio.create_task`.
Expand All @@ -192,10 +192,12 @@ 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:
if context is None and eager_start is None:
task = self._loop.create_task(coro, name=name)
else:
elif eager_start is None:
task = self._loop.create_task(coro, name=name, context=context)
else:
task = self._loop.create_task(coro, name=name, context=context, eager_start=eager_start)

# optimization: Immediately call the done callback if the task is
# already done (e.g. if the coro was able to complete eagerly),
Expand Down
12 changes: 8 additions & 4 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def __wakeup(self, future):
Task = _CTask = _asyncio.Task


def create_task(coro, *, name=None, context=None):
def create_task(coro, *, name=None, context=None, eager_start=None):
"""Schedule the execution of a coroutine object in a spawn task.
Return a Task object.
Expand All @@ -393,8 +393,10 @@ def create_task(coro, *, name=None, context=None):
if context is None:
# Use legacy API if context is not needed
task = loop.create_task(coro, name=name)
else:
elif eager_start is None:
task = loop.create_task(coro, name=name, context=context)
else:
task = loop.create_task(coro, name=name, context=context, eager_start)

return task

Expand Down Expand Up @@ -1008,9 +1010,11 @@ 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, *, name=None, context=None, eager_start=None):
if eager_start is None:
eager_start = True
return custom_task_constructor(
coro, loop=loop, name=name, context=context, eager_start=True)
coro, loop=loop, name=name, context=context, eager_start=eager_start)

return factory

Expand Down

0 comments on commit c809133

Please sign in to comment.