Course contentsShow
← Python
Lesson 895 of 1,06014. Concurrency and PerformanceFree lesson

Integrating Blocking Code with Run In Executor

Offload CPU-intensive or blocking legacy code to a thread or process pool without stalling the main event loop.

Integrating Blocking Code with Run In Executor

In this lesson, you will learn how to prevent blocking operations from freezing your entire asynchronous application by offloading them to external thread or process pools.

The Problem: Blocking the Event Loop

Asynchronous programming relies on the event loop to manage multiple tasks concurrently. However, if you execute a synchronous, CPU-intensive, or blocking I/O operation—such as a complex mathematical calculation or a legacy database call—directly inside a coroutine, the event loop stops. It cannot process other tasks until that code finishes, effectively turning your high-performance asynchronous system into a sequential one.

The Solution: run_in_executor

The loop.run_in_executor() method allows you to hand off these "blocking" tasks to a separate pool of workers. By default, this uses a ThreadPoolExecutor, but you can also provide a ProcessPoolExecutor if your task is strictly CPU-bound and needs to bypass the Global Interpreter Lock (GIL).

Here is how you integrate a blocking function into your asyncio code:

import asyncio
import time

def blocking_io_operation(data):
    # Imagine this is a slow legacy database call
    time.sleep(2) 
    return f"Processed: {data}"

async def main():
    loop = asyncio.get_running_loop()
    
    # Offload the blocking call to the default executor
    # This returns a Future that we can await
    result = await loop.run_in_executor(None, blocking_io_operation, "my_data")
    
    print(result)

asyncio.run(main())

How it Works

When you pass None as the first argument, asyncio uses its default ThreadPoolExecutor. The blocking function runs in a separate thread, allowing the event loop to continue switching between other coroutines while waiting for the result. Once the thread completes, the result is returned to your coroutine as if it were a standard await call.

By using run_in_executor, you ensure that legacy or blocking code acts like a "good citizen" in your asynchronous environment, maintaining high throughput for the rest of your application.

Key Takeaway: Use loop.run_in_executor to hand off blocking synchronous tasks to a worker pool, ensuring your event loop remains responsive and your concurrent tasks aren't stalled by legacy code.