Offload work with explicit loop executor integration.
Section: Threads and Blocking Code
Use `run_in_executor()`
python
python
import asyncio
import time
def blocking():
time.sleep(1)
return 42
async def main():
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(None, blocking)
print(result)
asyncio.run(main())Explanation
A lower-level alternative to `to_thread()` when you need more control.
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 Threads and Blocking Code
Run blocking I/O in a thread
Use `asyncio.to_thread()` for simple thread offloading.
Run a shell command asynchronously
Spawn a shell pipeline with `create_subprocess_shell()`.