Skip to content

Commit bf89d42

Browse files
authored
gh-97696 Remove unnecessary check for eager_start kwarg (#104188)
Instead, add docstring to create_eager_task_factory.
1 parent faf1962 commit bf89d42

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Lib/asyncio/tasks.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -942,18 +942,31 @@ def callback():
942942

943943

944944
def create_eager_task_factory(custom_task_constructor):
945+
"""Create a function suitable for use as a task factory on an event-loop.
945946
946-
if "eager_start" not in inspect.signature(custom_task_constructor).parameters:
947-
raise TypeError(
948-
"Provided constructor does not support eager task execution")
947+
Example usage:
948+
949+
loop.set_task_factory(
950+
asyncio.create_eager_task_factory(my_task_constructor))
951+
952+
Now, tasks created will be started immediately (rather than being first
953+
scheduled to an event loop). The constructor argument can be any callable
954+
that returns a Task-compatible object and has a signature compatible
955+
with `Task.__init__`; it must have the `eager_start` keyword argument.
956+
957+
Most applications will use `Task` for `custom_task_constructor` and in
958+
this case there's no need to call `create_eager_task_factory()`
959+
directly. Instead the global `eager_task_factory` instance can be
960+
used. E.g. `loop.set_task_factory(asyncio.eager_task_factory)`.
961+
"""
949962

950963
def factory(loop, coro, *, name=None, context=None):
951964
return custom_task_constructor(
952965
coro, loop=loop, name=name, context=context, eager_start=True)
953966

954-
955967
return factory
956968

969+
957970
eager_task_factory = create_eager_task_factory(Task)
958971

959972

0 commit comments

Comments
 (0)