Skip to content

asyncio.gather() not spawning threads concurrently #132541

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kushalj1997 opened this issue Apr 15, 2025 · 1 comment
Closed

asyncio.gather() not spawning threads concurrently #132541

kushalj1997 opened this issue Apr 15, 2025 · 1 comment
Labels
docs Documentation in the Doc dir

Comments

@kushalj1997
Copy link

kushalj1997 commented Apr 15, 2025

Documentation

I have a quick training loop written in Apple MLX that I want to spawn a thread using asyncio for, as well as a separate thread to plot losses and other values within the model. However, I don't see the second thread spawn when I run await asyncio.gather and the documentation mentions it schedules threads for concurrent execution.

A quick search of "does asyncio gather spawn threads in background" leads to this confusing answer - "operation within single-threaded environment" versus "coroutines scheduled concurrently within even loop".

asyncio.gather itself does not directly spawn threads. It operates within the single-threaded environment of the asyncio event loop. However, it can be used in conjunction with threads when necessary to perform blocking operations concurrently.
When you use asyncio.gather, it takes a series of awaitables (usually coroutines) and schedules them to run concurrently within the event loop. The event loop manages the execution of these awaitables, switching between them whenever one of them is waiting for I/O or another event. This concurrency is achieved through cooperative multitasking, not through the creation of new threads.
If you need to perform a blocking operation within an asyncio program (e.g., a long-running CPU-bound task or a blocking I/O call), you can use asyncio.to_thread or asyncio.run_in_executor to run the operation in a separate thread or process. This allows the blocking operation to run concurrently with other asyncio tasks without blocking the event loop.

What's the correct way to spawn concurrent threads? Do I actually just need processes, or will CPU schedulers run threads in parallel if hyper-threading is available? Should I be using asyncio.run() for all my tasks instead?

@kushalj1997 kushalj1997 added the docs Documentation in the Doc dir label Apr 15, 2025
@graingert
Copy link
Contributor

graingert commented Apr 15, 2025

Hello @kushalj1997! This is the CPython issue tracker and is best for issues and bugs

For questions please ask on the help forums: https://discuss.python.org/c/help/7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Development

No branches or pull requests

2 participants