From 703e78429bd2797746b5791af2e0bfb0561228f9 Mon Sep 17 00:00:00 2001 From: jbower <1978924+jbower-fb@users.noreply.github.com> Date: Thu, 4 May 2023 22:34:44 -0700 Subject: [PATCH 1/7] Remove unnecessary check for eager_start kwarg --- Lib/asyncio/tasks.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index aa5269ade19a7f..1f34d5d2bbaa76 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -932,17 +932,13 @@ def callback(): def create_eager_task_factory(custom_task_constructor): - if "eager_start" not in inspect.signature(custom_task_constructor).parameters: - raise TypeError( - "Provided constructor does not support eager task execution") - def factory(loop, coro, *, name=None, context=None): return custom_task_constructor( coro, loop=loop, name=name, context=context, eager_start=True) - return factory + eager_task_factory = create_eager_task_factory(Task) From 86c8b430abd123c99aab11cec73c0fc274662b02 Mon Sep 17 00:00:00 2001 From: Jacob Bower <1978924+jbower-fb@users.noreply.github.com> Date: Fri, 5 May 2023 18:09:36 -0700 Subject: [PATCH 2/7] Add docstring to create_eager_task_factory --- Lib/asyncio/tasks.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 1f34d5d2bbaa76..38aeaf547b4b24 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -931,6 +931,13 @@ def callback(): def create_eager_task_factory(custom_task_constructor): + """ + Creates a function suitable for use as a task factory on an event-loop. + The tasks created will be started immediately (rather than being first + scheduled to an event loop). The constructor argument can be any callable + that constructs a Task-compatible object and supports the eager_start + keyword argument. + """ def factory(loop, coro, *, name=None, context=None): return custom_task_constructor( From defcfe85b7f7045db96ad84f2b1f94de5f0a0b4d Mon Sep 17 00:00:00 2001 From: Jacob Bower <1978924+jbower-fb@users.noreply.github.com> Date: Fri, 5 May 2023 18:23:12 -0700 Subject: [PATCH 3/7] Improve create_eager_task_factory() docstring --- Lib/asyncio/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 38aeaf547b4b24..20d806ff833c18 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -935,8 +935,8 @@ def create_eager_task_factory(custom_task_constructor): Creates a function suitable for use as a task factory on an event-loop. The tasks created will be started immediately (rather than being first scheduled to an event loop). The constructor argument can be any callable - that constructs a Task-compatible object and supports the eager_start - keyword argument. + that returns a ``Task`` compatible object and has a signature compatible + with ``Task.__init__``, including the ``eager_start`` keyword argument. """ def factory(loop, coro, *, name=None, context=None): From 26409a2bb8eb3479251619694f61d24a6578cebf Mon Sep 17 00:00:00 2001 From: Jacob Bower <1978924+jbower-fb@users.noreply.github.com> Date: Fri, 5 May 2023 18:24:50 -0700 Subject: [PATCH 4/7] Make docstring PEP 257 compliant --- Lib/asyncio/tasks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 20d806ff833c18..32b2370fc4f5e6 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -933,6 +933,7 @@ def callback(): def create_eager_task_factory(custom_task_constructor): """ Creates a function suitable for use as a task factory on an event-loop. + The tasks created will be started immediately (rather than being first scheduled to an event loop). The constructor argument can be any callable that returns a ``Task`` compatible object and has a signature compatible From 8b5f6c5e7118747479675f422bb80b592d0c3f53 Mon Sep 17 00:00:00 2001 From: Jacob Bower <1978924+jbower-fb@users.noreply.github.com> Date: Mon, 8 May 2023 17:20:53 -0700 Subject: [PATCH 5/7] Further improve create_eager_task_factory docstring --- Lib/asyncio/tasks.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 32b2370fc4f5e6..9807001d45103d 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -931,14 +931,23 @@ def callback(): def create_eager_task_factory(custom_task_constructor): - """ - Creates a function suitable for use as a task factory on an event-loop. - - The tasks created will be started immediately (rather than being first - scheduled to an event loop). The constructor argument can be any callable - that returns a ``Task`` compatible object and has a signature compatible - with ``Task.__init__``, including the ``eager_start`` keyword argument. - """ + """Create a function suitable for use as a task factory on an event-loop. + + Example usage: + + loop.set_task_factory( + asyncio.create_eager_task_factory(my_task_constructor)) + + Now, tasks created will be started immediately (rather than being first + scheduled to an event loop). The constructor argument can be any callable + that returns a Task-compatible object and has a signature compatible + with `Task.__init__`; it must have the `eager_start` keyword argument. + + Most applications will use `Task` for `my_task_constructor` and in this + case there's no need to call `create_eager_task_factory()` directly. + Instead the global `eager_task_factory` instance can be used. E.g. + `loop.set_task_factory(asyncio.eager_task_factory)`. + """ def factory(loop, coro, *, name=None, context=None): return custom_task_constructor( From fe94a2bf407b765e690be8c3f9d3e421081e6ffe Mon Sep 17 00:00:00 2001 From: Jacob Bower <1978924+jbower-fb@users.noreply.github.com> Date: Mon, 8 May 2023 17:22:25 -0700 Subject: [PATCH 6/7] Further docstring refinement --- Lib/asyncio/tasks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 9807001d45103d..a7510fa1ad282d 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -943,10 +943,10 @@ def create_eager_task_factory(custom_task_constructor): that returns a Task-compatible object and has a signature compatible with `Task.__init__`; it must have the `eager_start` keyword argument. - Most applications will use `Task` for `my_task_constructor` and in this - case there's no need to call `create_eager_task_factory()` directly. - Instead the global `eager_task_factory` instance can be used. E.g. - `loop.set_task_factory(asyncio.eager_task_factory)`. + Most applications will use `Task` for `custom_task_constructor` and in + this case there's no need to call `create_eager_task_factory()` + directly. Instead the global `eager_task_factory` instance can be + used. E.g. `loop.set_task_factory(asyncio.eager_task_factory)`. """ def factory(loop, coro, *, name=None, context=None): From 68fff101c254b45b5405013736be288e93282ad2 Mon Sep 17 00:00:00 2001 From: Jacob Bower <1978924+jbower-fb@users.noreply.github.com> Date: Mon, 8 May 2023 17:25:00 -0700 Subject: [PATCH 7/7] Remove trailing whitespace --- Lib/asyncio/tasks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index a7510fa1ad282d..170d6957cdfc52 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -932,17 +932,17 @@ def callback(): def create_eager_task_factory(custom_task_constructor): """Create a function suitable for use as a task factory on an event-loop. - + Example usage: - + loop.set_task_factory( asyncio.create_eager_task_factory(my_task_constructor)) - + Now, tasks created will be started immediately (rather than being first scheduled to an event loop). The constructor argument can be any callable that returns a Task-compatible object and has a signature compatible with `Task.__init__`; it must have the `eager_start` keyword argument. - + Most applications will use `Task` for `custom_task_constructor` and in this case there's no need to call `create_eager_task_factory()` directly. Instead the global `eager_task_factory` instance can be