Python asyncio Advanced/Schedule from another thread

Use `call_soon_threadsafe()` when another thread must notify the loop.

Section: Event Loop Management

Schedule from another thread

python
python
import asyncio
import threading

async def main():
    loop = asyncio.get_running_loop()
    event = asyncio.Event()

    def notify():
        loop.call_soon_threadsafe(event.set)

    threading.Thread(target=notify).start()
    await event.wait()
    print("notified")

asyncio.run(main())
Explanation

This is the thread-safe callback bridge into the event loop.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Event Loop Management
Submit a coroutine from another thread
Use `run_coroutine_threadsafe()` with a loop reference.
OpenIn sheetpythonsame section
Schedule a callback later
Run a plain callback after a delay.
OpenIn sheetpythonsame section
Prefer `get_running_loop()` in async code
Modern replacement for older loop access patterns.
Know the file I/O caveat
Document the limitation around file descriptor watching for ordinary file I/O.