liveBook · Manning

Asyncio.run_coroutine_threadsafe

As explained in Python Concurrency with asyncio

asyncio.run_coroutine_threadsafe is an asyncio function that submits a coroutine to an event loop from another thread and returns a concurrent.futures future.

asyncio.run_coroutine_threadsafe submits a coroutine to an event loop from another thread without blocking, then immediately returns a concurrent.futures future for accessing its result. section 7.4.2

Unlike asyncio.run, which waits for completion, this function keeps a Tkinter interface responsive while an asyncio loop runs in a separate thread. Its returned future is thread-safe because asyncio futures are not. section 7.4.2

Submitting a stress test

A StressTest.start method calls asyncio.run_coroutine_threadsafe(self._make_requests(), self._loop) and stores the returned future while the asyncio loop performs URL requests.

How Python Concurrency with asyncio covers it

How deep it goes
Explains submission, nonblocking behavior, thread safety, and future type through a stress-test implementation.
Examples
Uses a Tkinter URL requester and StressTest class to submit asynchronous URL requests and track progress.
What it assumes
Assumes familiarity with coroutines, event loops, and threads while contrasting asyncio and concurrent.futures futures.
What you can do after
Apply it via asyncio to launch a coroutine from another thread without freezing a Tkinter interface.
How it connects
Connects coroutines with event loops, threads, concurrent.futures futures, Tkinter, and call_soon_threadsafe.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.