René's URL Explorer Experiment


Title: asynciomodule.c performance degradation in free-threading under heavy load · Issue #144337 · python/cpython · GitHub

Open Graph Title: asynciomodule.c performance degradation in free-threading under heavy load · Issue #144337 · python/cpython

X Title: asynciomodule.c performance degradation in free-threading under heavy load · Issue #144337 · python/cpython

Description: Bug report Bug description: We have asyncio Python servers we are trying to run under free-threading that create event loops. Under, say, 800 QPS we noticed performance regressions relative to the GIL build and, upon tracing, most of the...

Open Graph Description: Bug report Bug description: We have asyncio Python servers we are trying to run under free-threading that create event loops. Under, say, 800 QPS we noticed performance regressions relative to the ...

X Description: Bug report Bug description: We have asyncio Python servers we are trying to run under free-threading that create event loops. Under, say, 800 QPS we noticed performance regressions relative to the ...

Opengraph URL: https://github.com/python/cpython/issues/144337

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asynciomodule.c performance degradation in free-threading under heavy load","articleBody":"# Bug report\n\n### Bug description:\n\nWe have asyncio Python servers we are trying to run under free-threading that create event loops. Under, say, 800 QPS we noticed performance regressions relative to the GIL build and, upon tracing, most of the regression is in the `cancel_all_tasks` call when it calls the C implementation to get all tasks for the loop in question. My LLM suggested that the performance bottleneck is in the `_PyEval_StopTheWorld` call required. It suggested an alternative implementation using per-thread task buckets to avoid needing to stop the world.\n\nIt created the following repro script to show the performance issue. With its suggested changes it creates 10X loops and increases churn rate 83X under free-threading (changes would be ifdef-guarded to avoid a 10X regression in loop churn rate under standard build).\n\nI still need to validate the changes it proposed but, without doing something, we would have to avoid asyncio + free-threading because of this.\n\n```python\n\nimport asyncio\nimport threading\nimport time\nimport argparse\nimport sys\n\n# Configuration\nNUM_CHURN_THREADS = 20  # Number of threads creating/destroying loops\nRUN_DURATION = 10       # Seconds to run\nTARGET_LOOPS_PER_SEC = 800 # Total target loops per second across all threads\n\nstop_event = threading.Event()\n\ndef churn_worker(worker_id):\n    \"\"\"Continuously creates and destroys event loops.\"\"\"\n    count = 0\n    while not stop_event.is_set():\n        # asyncio.run creates a new event loop, runs the coroutine, and closes it.\n        asyncio.run(asyncio.sleep(0))\n        count += 1\n        # Optional: slight delay to throttle if needed, but we want max contention for this demo\n        # time.sleep(0.001)\n    return count\n\nasync def monitor_coro():\n    \"\"\"Calls all_tasks continuously.\"\"\"\n    count = 0\n    start_time = time.time()\n    while not stop_event.is_set():\n        # This triggers the scan of all tasks in all threads\n        tasks = asyncio.all_tasks()\n        count += 1\n        # Yield to allow other things to happen on this loop, though we mostly care about the scan cost\n        await asyncio.sleep(0)\n    \n    duration = time.time() - start_time\n    return count, duration\n\ndef monitor_worker(results_list):\n    \"\"\"Runs a persistent loop that calls all_tasks.\"\"\"\n    try:\n        count, duration = asyncio.run(monitor_coro())\n        results_list.append((count, duration))\n    except Exception as e:\n        print(f\"Monitor failed: {e}\")\n\ndef main():\n    parser = argparse.ArgumentParser(description=\"Asyncio Performance Demo\")\n    parser.add_argument(\"--threads\", type=int, default=NUM_CHURN_THREADS, help=\"Number of churn threads\")\n    parser.add_argument(\"--duration\", type=int, default=RUN_DURATION, help=\"Duration in seconds\")\n    args = parser.parse_args()\n\n    print(f\"Starting generic asyncio benchmark on {sys.version}...\")\n    print(f\"Configuration: {args.threads} churn threads, {args.duration}s duration.\")\n    \n    threads = []\n    \n    # Start churn threads\n    # We use a ThreadPoolExecutor or just raw threads. Raw threads are fine.\n    # To measure iterations, we can use a mutable list or class\n    churn_counts = [0] * args.threads\n    \n    def wrapped_churn(idx):\n        churn_counts[idx] = churn_worker(idx)\n\n    for i in range(args.threads):\n        t = threading.Thread(target=wrapped_churn, args=(i,))\n        t.start()\n        threads.append(t)\n\n    # Start monitor thread\n    monitor_results = []\n    monitor_thread = threading.Thread(target=monitor_worker, args=(monitor_results,))\n    monitor_thread.start()\n    threads.append(monitor_thread)\n\n    # Run for specified duration\n    try:\n        time.sleep(args.duration)\n    except KeyboardInterrupt:\n        pass\n    finally:\n        stop_event.set()\n\n    # Join all\n    for t in threads:\n        t.join()\n\n    # Aggregate results\n    total_loops = sum(churn_counts)\n    loops_per_sec = total_loops / args.duration\n    \n    monitor_calls, mon_duration =0, 1\n    if monitor_results:\n        monitor_calls, mon_duration = monitor_results[0]\n    \n    all_tasks_per_sec = monitor_calls / mon_duration if mon_duration > 0 else 0\n\n    print(\"-\" * 40)\n    print(f\"Results:\")\n    print(f\"  Total Event Loops Created: {total_loops}\")\n    print(f\"  Loop Churn Rate:           {loops_per_sec:.2f} loops/sec\")\n    print(f\"  all_tasks() Calls:         {monitor_calls}\")\n    print(f\"  all_tasks() Rate:          {all_tasks_per_sec:.2f} calls/sec\")\n    print(\"-\" * 40)\n    print(\"Interpretation:\")\n    print(\"  Higher 'Loop Churn Rate' indicates less blocking during thread/task destruction.\")\n    print(\"  Higher 'all_tasks() Rate' indicates faster scanning of tasks.\")\n    print(\"  On unoptimized Python (with StopTheWorld), these numbers should be significantly lower\")\n    print(\"  concurrently due to the global pause.\")\n\nif __name__ == \"__main__\":\n    main()\n```\n\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nLinux","author":{"url":"https://github.com/jgrave3","@type":"Person","name":"jgrave3"},"datePublished":"2026-01-30T11:01:29Z","url":"https://github.com/python/cpython/issues/144337"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:5478ec16-b722-05ca-6c74-1757e9ea69b7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id86C2:16AB79:4593EDD:5844E15:6B023A94
html-safe-nonce2f5fa2a5052dafe4bfa498130b760c3d8c8ae49792c509475eb1a499f6fe0d3f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkMyOjE2QUI3OTo0NTkzRUREOjU4NDRFMTU6NkIwMjNBOTQiLCJ2aXNpdG9yX2lkIjoiODA5MDM3MDQwNzY3ODQ5MTI4NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacdd80495eff0d275499cd3db500fc56e17b8c3eb62a66e0f04825bba7ecc08508
hovercard-subject-tagissue:3875182073
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/144337/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c96ec02a105d3c49c47c13d9b2c0601a721e06edfe97cd5bcc523679358b3664/python/cpython/issues/144337
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c96ec02a105d3c49c47c13d9b2c0601a721e06edfe97cd5bcc523679358b3664/python/cpython/issues/144337
og:image:altBug report Bug description: We have asyncio Python servers we are trying to run under free-threading that create event loops. Under, say, 800 QPS we noticed performance regressions relative to the ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejgrave3
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/144337#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F144337
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F144337
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/144337
Reloadhttps://github.com/python/cpython/issues/144337
Reloadhttps://github.com/python/cpython/issues/144337
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/144337
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/144337
asynciomodule.c performance degradation in free-threading under heavy loadhttps://github.com/python/cpython/issues/144337#top
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
https://github.com/jgrave3
jgrave3https://github.com/jgrave3
on Jan 30, 2026https://github.com/python/cpython/issues/144337#issue-3875182073
jgrave3https://github.com/jgrave3
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Jan 30, 2026https://github.com/python/cpython/issues/144337#event-22404377056
sergey-miryanovhttps://github.com/sergey-miryanov
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
on Jan 30, 2026https://github.com/python/cpython/issues/144337#event-22404508108
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Jan 30, 2026https://github.com/python/cpython/issues/144337#event-22404510413
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Jan 30, 2026https://github.com/python/cpython/issues/144337
sergey-miryanovhttps://github.com/sergey-miryanov
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
on Jan 30, 2026https://github.com/python/cpython/issues/144337#event-22404512023
StanFromIrelandhttps://github.com/StanFromIreland
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
on Jan 30, 2026https://github.com/python/cpython/issues/144337#event-22411471507
https://github.com/Muawiya-contact
Muawiya-contacthttps://github.com/Muawiya-contact
on Jan 30, 2026https://github.com/python/cpython/issues/144337#issuecomment-3825863083
@sergey-miryanovhttps://github.com/sergey-miryanov
@StanFromIrelandhttps://github.com/StanFromIreland
https://github.com/StanFromIreland
StanFromIrelandhttps://github.com/StanFromIreland
on Jan 30, 2026https://github.com/python/cpython/issues/144337#issuecomment-3825875082
https://github.com/Muawiya-contact
Muawiya-contacthttps://github.com/Muawiya-contact
on Jan 31, 2026https://github.com/python/cpython/issues/144337#issuecomment-3827521513
@StanFromIrelandhttps://github.com/StanFromIreland
https://github.com/picnixz
picnixzhttps://github.com/picnixz
on Jan 31, 2026https://github.com/python/cpython/issues/144337#issuecomment-3829465756
https://github.com/kumaraditya303
kumaraditya303https://github.com/kumaraditya303
on Feb 1, 2026https://github.com/python/cpython/issues/144337#issuecomment-3831195349
kumaraditya303https://github.com/kumaraditya303
pendingThe issue will be closed if no feedback is providedhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22pending%22
on Feb 1, 2026https://github.com/python/cpython/issues/144337#event-22444689583
https://github.com/jgrave3
jgrave3https://github.com/jgrave3
on Feb 2, 2026https://github.com/python/cpython/issues/144337#issuecomment-3836463796
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Feb 2, 2026https://github.com/python/cpython/issues/144337#issuecomment-3836968662
https://github.com/jgrave3
jgrave3https://github.com/jgrave3
on Feb 3, 2026https://github.com/python/cpython/issues/144337#issuecomment-3840548935
https://github.com/python/cpython/blob/main/Lib/asyncio/runners.py#L208https://github.com/python/cpython/blob/main/Lib/asyncio/runners.py#L208
https://github.com/python/cpython/blob/main/Modules/_asynciomodule.c#L4067https://github.com/python/cpython/blob/main/Modules/_asynciomodule.c#L4067
…https://github.com/python/cpython/issues/144337
python/cpython#144337https://github.com/python/cpython/issues/144337
#144337 (comment)https://github.com/python/cpython/issues/144337#issuecomment-3836968662
#144337 (comment)https://github.com/python/cpython/issues/144337#issuecomment-3836968662
https://github.com/notifications/unsubscribe-auth/BIJZGJOIT3XVMH6MIR3YZCL4J6KG3AVCNFSM6AAAAACTNRVNZ2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTQMZWHE3DQNRWGIhttps://github.com/notifications/unsubscribe-auth/BIJZGJOIT3XVMH6MIR3YZCL4J6KG3AVCNFSM6AAAAACTNRVNZ2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTQMZWHE3DQNRWGI
https://github.com/kumaraditya303
kumaraditya303https://github.com/kumaraditya303
on Feb 3, 2026https://github.com/python/cpython/issues/144337#issuecomment-3840569332
https://github.com/python/cpython/blob/main/Modules/_asynciomodule.c#L4067https://github.com/python/cpython/blob/main/Modules/_asynciomodule.c#L4067
https://github.com/jgrave3
jgrave3https://github.com/jgrave3
on Feb 4, 2026https://github.com/python/cpython/issues/144337#issuecomment-3848377635
…https://github.com/python/cpython/issues/144337
python/cpython#144337https://github.com/python/cpython/issues/144337
#144337 (comment)https://github.com/python/cpython/issues/144337#issuecomment-3840569332
https://github.com/python/cpython/blob/main/Modules/_asynciomodule.c#L4067https://github.com/python/cpython/blob/main/Modules/_asynciomodule.c#L4067
#144337 (comment)https://github.com/python/cpython/issues/144337#issuecomment-3840569332
https://github.com/notifications/unsubscribe-auth/BIJZGJKISZW3FHRHC7R2K634KB4HRAVCNFSM6AAAAACTNRVNZ2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTQNBQGU3DSMZTGIhttps://github.com/notifications/unsubscribe-auth/BIJZGJKISZW3FHRHC7R2K634KB4HRAVCNFSM6AAAAACTNRVNZ2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTQNBQGU3DSMZTGI
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Feb 4, 2026https://github.com/python/cpython/issues/144337#issuecomment-3848601260
@kumaraditya303https://github.com/kumaraditya303
#140542https://github.com/python/cpython/pull/140542
kumaraditya303https://github.com/kumaraditya303
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
pendingThe issue will be closed if no feedback is providedhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22pending%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Feb 5, 2026https://github.com/python/cpython/issues/144337#event-22547644378
https://github.com/kumaraditya303
kumaraditya303https://github.com/kumaraditya303
on Feb 5, 2026https://github.com/python/cpython/issues/144337#issuecomment-3851106509
#140542https://github.com/python/cpython/pull/140542
https://github.com/kumaraditya303
kumaraditya303https://github.com/kumaraditya303
on Feb 5, 2026https://github.com/python/cpython/issues/144337#issuecomment-3853313561
#144494https://github.com/python/cpython/pull/144494
https://github.com/kumaraditya303
kumaraditya303https://github.com/kumaraditya303
on Feb 15, 2026https://github.com/python/cpython/issues/144337#issuecomment-3903381363
kumaraditya303https://github.com/kumaraditya303
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Feb 15, 2026https://github.com/python/cpython/issues/144337#event-22799985772
github-project-automationhttps://github.com/apps/github-project-automation
asynciohttps://github.com/orgs/python/projects/29
on Feb 15, 2026https://github.com/python/cpython/issues/144337
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/144337
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/144337
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
asynciohttps://github.com/orgs/python/projects/29
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.