Running Synchronous Blocking Code via asyncio.to_thread
Calling a synchronous blocking function (e.g. `time.sleep` or legacy disk I/O) directly inside a coroutine freezes the single-threaded event loop. Python 3.9+ introduced `asyncio.to_thread()` to delegate blocking calls to a worker thread pool without stalling other tasks ๐งตโก.
import asyncio
import time
def synchronous_heavy_io(path):
# Synchronous blocking call:
time.sleep(0.02)
return f"PAYLOAD_FROM_{path}"
async def main():
# Offload to thread pool without blocking event loop:
result = await asyncio.to_thread(synchronous_heavy_io, "/var/data.bin")
print(f"Received: {result} ๐")
asyncio.run(main())
# 25. Running Synchronous Blocking Code via asyncio.to_thread The cardinal sin of async programming is calling blocking code inside the event loop thread. A single `requests.get()` or `time.sleep()` blocks the entire process, turning your high-concurrency server into a frozen brick ๐. ```python import asyncio import time def blocking_calc(x): time.sleep(0.05) return x ** 2 async def main(): # Asynchronously awaits worker thread execution: val = await asyncio.to_thread(blocking_calc, 12) ``` ### Thread Delegation Architecture 1. **Thread Pool Offloading**: `asyncio.to_thread()` submits the callable to the default `ThreadPoolExecutor` behind the scenes. 2. **Context Preservation**: Unlike raw `loop.run_in_executor()`, `asyncio.to_thread()` automatically copies the calling task's `contextvars.copy_context()` into the worker thread, ensuring trace IDs and credentials remain intact ๐งฌ. 3. **Loop Freedom**: The event loop thread immediately returns to servicing other network sockets, polling queues, and running ready coroutines while the thread churns in the background ๐๏ธ. --- ### Interactive Pop-Quiz ๐ง #### What catastrophe occurs if you run a slow synchronous disk read directly inside an async coroutine without offloading? - [ ] The single-threaded event loop freezes completely, starving all other concurrent coroutines until the read finishes ๐ - [ ] Python raises an AsyncBlockError exception immediately - [ ] CPython automatically converts the synchronous function to WebAssembly - [ ] The operating system drops the process priority to idle --- ### Practical Challenge ๐ฏ Define a synchronous function `blocking_hash(data)` that blocks using `time.sleep(0.01)` and returns the length of `data`. In an async `main()`, run three calls concurrently using `asyncio.to_thread()` inside `asyncio.gather()`, compute their sum, and print it. <details> <summary>Solution</summary> ```python import asyncio import time def blocking_hash(data): time.sleep(0.01) return len(data) async def main(): inputs = ["alpha", "beta", "gamma"] tasks = [asyncio.to_thread(blocking_hash, item) for item in inputs] lengths = await asyncio.gather(*tasks) print(sum(lengths)) asyncio.run(main()) ``` </details>