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
Domain: github.com
{"@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-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5478ec16-b722-05ca-6c74-1757e9ea69b7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86C2:16AB79:4593EDD:5844E15:6B023A94 |
| html-safe-nonce | 2f5fa2a5052dafe4bfa498130b760c3d8c8ae49792c509475eb1a499f6fe0d3f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkMyOjE2QUI3OTo0NTkzRUREOjU4NDRFMTU6NkIwMjNBOTQiLCJ2aXNpdG9yX2lkIjoiODA5MDM3MDQwNzY3ODQ5MTI4NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | dd80495eff0d275499cd3db500fc56e17b8c3eb62a66e0f04825bba7ecc08508 |
| hovercard-subject-tag | issue:3875182073 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/144337/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c96ec02a105d3c49c47c13d9b2c0601a721e06edfe97cd5bcc523679358b3664/python/cpython/issues/144337 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c96ec02a105d3c49c47c13d9b2c0601a721e06edfe97cd5bcc523679358b3664/python/cpython/issues/144337 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jgrave3 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width