call_soon_threadsafe schedules a regular Python function on an asyncio event loop from another thread without blocking. It is useful when a thread must safely request work from the loop thread. section 7.4.2
Use it with these distinctions in mind:
- Pass a regular function, not a coroutine; the callback runs on the next iteration of the event loop. section 7.4.2
- Use asyncio.run_coroutine_threadsafe for a coroutine; it immediately returns a concurrent.futures Future. section 7.4.2
- Avoid asyncio.run from a UI thread because it blocks until completion and can hang the application. section 7.4.2
Updating a threaded UI workflow
A Tkinter application can use a callback to request progress updates while an asyncio event loop runs in a separate thread. call_soon_threadsafe safely schedules the regular update function on that loop. section 7.4.2
Figure 7.5 The tk event loop submits a task to the asyncio event loop, which runs in a separate thread.
The event-loop diagram clarifies how a Tkinter loop submits work to an asyncio loop running in another thread. section 7.4.2
