Python asyncio Advanced/Submit a coroutine from another thread

Use `run_coroutine_threadsafe()` with a loop reference.

Section: Event Loop Management

Submit a coroutine from another thread

python
python
import asyncio
import threading

async def echo():
    return "ok"

async def main():
    loop = asyncio.get_running_loop()
    result_holder = {}

    def worker():
        future = asyncio.run_coroutine_threadsafe(echo(), loop)
        result_holder["value"] = future.result()

    t = threading.Thread(target=worker)
    t.start()
    await asyncio.sleep(0.1)
    t.join()
    print(result_holder["value"])

asyncio.run(main())
Explanation

Useful when integrating threaded code with an existing 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
Schedule from another thread
Use `call_soon_threadsafe()` when another thread must notify the loop.
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.