Skip to content

asyncio.gather() not spawning threads concurrently #132541

@kushalj1997

Description

@kushalj1997

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions