FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-128308: pass **kwargs to asyncio task_factory by graingert · Pull Request #128768 · python/cpython · GitHub

Repository navigation

gh-128308: pass **kwargs to asyncio task_factory - #128768

Merged
kumaraditya303 merged 20 commits into
python:mainfrom
graingert:pass-kwargs-to-task-factory
Jan 20, 2025
Merged

kumaraditya303 merged 20 commits into
python:mainfrom
graingert:pass-kwargs-to-task-factory

Conversation

graingert commented Jan 13, 2025 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

graingert force-pushed the pass-kwargs-to-task-factory branch from 5b7bf7b to 0da18e8 Compare January 13, 2025 07:49
graingert marked this pull request as ready for review January 13, 2025 15:31
Comment thread Lib/asyncio/taskgroups.py Outdated
Comment thread Lib/asyncio/tasks.py
Comment thread Lib/asyncio/base_events.py Outdated
Comment thread Lib/asyncio/events.py Outdated

graingert commented Jan 20, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

We were somewhat worried about the performance of creating a dict with kwargs and then unpacking it, but it seems pretty similar if not faster:

It's a bit slower in the case where we have a name and a factory, which is a bit odd, but most of the time you'd be using a Loop subclass not a factory if you cared about this level of performance.

import pyperf


runner = pyperf.Runner()
runner.timeit(
    name="create a task with a name",
    stmt="loop.create_task(coro(), name='hello')",
    setup=["import asyncio; loop = asyncio.EventLoop()", "async def coro(): pass"],
    teardown="loop.call_soon(loop.stop); loop.run_forever(); loop.close()",
)
runner.timeit(
    name="create a task without a name",
    stmt="loop.create_task(coro())",
    setup=["import asyncio; loop = asyncio.EventLoop()", "async def coro(): pass"],
    teardown="loop.call_soon(loop.stop); loop.run_forever(); loop.close()",
)
runner.timeit(
    name="create a task with a name and factory",
    stmt="loop.create_task(coro(), name='hello')",
    setup=[
        "import asyncio; loop = asyncio.EventLoop()",
        "def task_factory(loop, coro, **kwargs): return asyncio.Task(coro, loop=loop, **kwargs)",
        "loop.set_task_factory(task_factory)",
        "async def coro(): pass",
    ],
    teardown="loop.call_soon(loop.stop); loop.run_forever(); loop.close()",
)
runner.timeit(
    name="create a task without a name, with a factory",
    stmt="loop.create_task(coro())",
    setup=[
        "import asyncio; loop = asyncio.EventLoop()",
        "def task_factory(loop, coro, **kwargs): return asyncio.Task(coro, loop=loop, **kwargs)",
        "loop.set_task_factory(task_factory)",
        "async def coro(): pass",
    ],
    teardown="loop.call_soon(loop.stop); loop.run_forever(); loop.close()",
)

before: 6f167d7

.....................
create a task with a name: Mean +- std dev: 1.27 us +- 0.10 us
.....................
create a task without a name: Mean +- std dev: 1.29 us +- 0.10 us
.....................
create a task with a name and factory: Mean +- std dev: 1.28 us +- 0.10 us
.....................
create a task without a name, with a factory: Mean +- std dev: 1.34 us +- 0.10 us

after: 539d2e9

.....................
create a task with a name: Mean +- std dev: 1.25 us +- 0.09 us
.....................
create a task without a name: Mean +- std dev: 1.22 us +- 0.10 us
.....................
create a task with a name and factory: Mean +- std dev: 1.53 us +- 0.11 us
.....................
create a task without a name, with a factory: Mean +- std dev: 1.32 us +- 0.10 us

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 merged commit 38a9956 into python:main Jan 20, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 21, 2025
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
asvetlov added the needs backport to 3.13 only security fixes label Feb 13, 2025

Copy link
Copy Markdown

Thanks @graingert for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

Copy link
Copy Markdown

Sorry, @graingert and @kumaraditya303, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 38a99568763604ccec5d5027f0658100ad76876f 3.13

bedevere-app Bot commented Feb 13, 2025

Copy link
Copy Markdown

GH-130084 is a backport of this pull request to the 3.13 branch.

bedevere-app Bot removed the needs backport to 3.13 only security fixes label Feb 13, 2025
asvetlov pushed a commit to asvetlov/cpython that referenced this pull request Feb 13, 2025
…onGH-128768)

(cherry picked from commit 38a9956)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 added a commit that referenced this pull request Feb 14, 2025
…#130084)

* [3.13] gh-128308: pass `**kwargs` to asyncio task_factory (GH-128768)
(cherry picked from commit 38a9956)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>


---------

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
graingert deleted the pass-kwargs-to-task-factory branch March 20, 2025 16:33

johng commented Apr 29, 2025

Copy link
Copy Markdown
Contributor

why did we break the API spec in a patch release? This caused a sudden breakage for us

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL