Using synchronous operating system calls like os.wait()
, os.waitpid()
, or similar functions in asynchronous code blocks
the entire event loop. This undermines the primary advantage of asynchronous programming - the ability to perform concurrent operations without
blocking execution.
When an async function makes a synchronous OS call:
- The event loop is completely blocked until the OS operation completes
- No other coroutines can run during this time, even if they’re ready to execute
- The responsiveness of the application is degraded
- In server applications, this can cause timeouts or failures for other concurrent requests
Instead, async libraries provide mechanisms to run blocking operations in separate threads without blocking the event loop:
-
asyncio.loop.run_in_executor()
for asyncio
-
trio.to_thread.run_sync()
for Trio
-
anyio.to_thread.run_sync()
for AnyIO
Using these constructs allows other tasks to continue executing while waiting for the blocking OS call to complete.