Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-128002: use per threads tasks linked list in asyncio#128869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ambv merged 22 commits intopython:mainfromkumaraditya303:per-thread-tasks
Feb 6, 2025

Conversation

kumaraditya303
Copy link
Contributor

@kumaraditya303kumaraditya303 commentedJan 15, 2025
edited
Loading

Use per-thread linked list of tasks in asyncio. This design allows for lock free register/unregister of tasks of loops running concurrently in different threads. It uses the stop the world pause to traverse the list of tasks from all threads from the thread whereall_tasks is called. This has no performance impact on regular builds as perbenchmarks and performs a bit faster on free-threadingbenchmarks. pyperformance benchmarks aren't good for this because it uses just one thread so there is little lock contention, this however performs much better when multiple threads are running.

On free-threading:

Benchmarkbm-20250111-linux-x86_64-python-3a570c6d58bd5ad7d7c1-3.14.0a3+-3a570c6bm-20250112-linux-x86_64-kumaraditya303-per_thread_tasks-3.14.0a3+-cca4057
async_tree_cpu_io_mixed_tg556 ms536 ms: 1.04x faster
async_tree_none_tg303 ms294 ms: 1.03x faster
coroutines26.2 ms25.5 ms: 1.03x faster
async_tree_memoization_tg397 ms387 ms: 1.03x faster
async_tree_cpu_io_mixed598 ms583 ms: 1.03x faster
async_generators498 ms486 ms: 1.02x faster
async_tree_io748 ms733 ms: 1.02x faster
async_tree_io_tg696 ms682 ms: 1.02x faster
async_tree_memoization442 ms436 ms: 1.02x faster
async_tree_none349 ms344 ms: 1.01x faster
Geometric mean(ref)1.02x faster

Benchmark hidden because not significant (1): asyncio_websockets

@1st1
Copy link
Member

First, great work on this, this is legitimately a cool PR.

That said, I'm feeling really uneasy about_PyEval_StopTheWorld. Maybe instead of this approach we try spin locks + a custom mini hash map data structure? That would obviously be a lot more work, but a cleaner and ultimately better perf approach.

Also please wait for reviews from@pablogsal and@ambv. I'm curious if this would make external introspection harder.

ambv reacted with thumbs up emoji

@graingert
Copy link
Contributor

Does this work where an event loop is used on one thread, stopped then resumed on another thread?

@graingert

This comment was marked as resolved.

@kumaraditya303
Copy link
ContributorAuthor

I have pushed some more changes:

  • Added a interpreter list of tasks which gets used when a task is alive but the thread state gets deallocated as suggested by@colesbury
  • Added thread id tracking in free-threading builds to avoid locking in the general case of loops running independently in threads without sharing tasks
  • Added some tests

TODO: benchmark it before merging

pablogsal
pablogsal previously requested changesJan 22, 2025
Copy link
Member

@pablogsalpablogsal left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If I am not mistaken this solution seems incompatible with the asyncio introspecction workflow we are adding on#124640. Please, ensure that this change is compatible with the changes in that PR to avoid problems in the future.

@bedevere-app
Copy link

When you're done making the requested changes, leave the comment:I have made the requested changes; please review again.

@kumaraditya303
Copy link
ContributorAuthor

If I am not mistaken this solution seems incompatible with the asyncio introspecction workflow we are adding on#124640. Please, ensure that this change is compatible with the changes in that PR to avoid problems in the future.

This PR has nothing to do with asyncio introspection. As I said in other PR, the change which would affect that is moving current task to per-loop which isn't done in this PR.

@pablogsal
Copy link
Member

Hummm, I must have misread how this affects the task management. Let me dismiss my request for changes meanwhile. Thanks for the patience with this@kumaraditya303!

@kumaraditya303
Copy link
ContributorAuthor

@colesbury PTAL

Copy link
Contributor

@colesburycolesbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This LGTM, but would you please also get this reviewed by another asyncio expert?

add_tasks_interp(PyInterpreterState *interp, PyListObject *tasks)
{
#ifdef Py_GIL_DISABLED
assert(interp->stoptheworld.world_stopped);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍🏻

@ambvambv merged commit0d68b14 intopython:mainFeb 6, 2025
41 checks passed
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull requestFeb 7, 2025
@kumaraditya303kumaraditya303 deleted the per-thread-tasks branchFebruary 7, 2025 11:59
cmaloney pushed a commit to cmaloney/cpython that referenced this pull requestFeb 8, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ambvambvambv left review comments

@colesburycolesburycolesbury left review comments

@pablogsalpablogsalpablogsal left review comments

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrentlyericsnowcurrently is a code owner

@1st11st1Awaiting requested review from 1st11st1 is a code owner

@asvetlovasvetlovAwaiting requested review from asvetlovasvetlov is a code owner

@willingcwillingcAwaiting requested review from willingcwillingc is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@kumaraditya303@1st1@graingert@pablogsal@ambv@colesbury

[8]ページ先頭

©2009-2025 Movatter.jp