Sling Academy
Home/Python/How to run Python code in multi-core CPUs using asyncio

How to run Python code in multi-core CPUs using asyncio

Last updated: February 12, 2024

Overview

Python’s asyncio is a powerful library for writing single-threaded concurrent code using coroutines. It is particularly useful for I/O-bound and high-level structured network code. However, by default, asyncio runs tasks in a single-threaded, single-process event loop, which might not be able to fully utilize the computing resources available on multi-core CPUs. This article will walk you through how to leverage asyncio in Python 3.11 or above to run code on multi-core CPUs efficiently.

Understanding asyncio

First, let’s delve into what asyncio is. Asyncio stands for Asynchronous I/O. It is a coroutine-based library for writing asynchronous programs in Python. It provides a way to execute code in an asynchronous manner, allowing your program to switch tasks efficiently, particularly in I/O-bound operations without blocking the program’s execution.

Why Multi-core?

Multi-core CPUs can execute multiple processes or threads simultaneously, dramatically improving the performance of compute-intensive operations. Despite asyncio’s efficiency in handling asynchronous I/O tasks, to leverage multi-core CPUs, you need to combine it with processes or threads that can run concurrently in separate cores. Python 3.11 introduced improvements and new features in its concurrent programming capabilities, making it easier and more efficient to use along with asyncio.

Preparing Your Environment

Ensure you have Python 3.11 or higher installed on your system. This version introduces several enhancements that benefit asyncio and concurrent programming. Check your Python version by running python --version command in your terminal.

Using Asyncio with Threads

Though asyncio is not inherently multi-threaded, it can be combined with the concurrent.futures.ThreadPoolExecutor for running CPU-bound tasks in a pool of threads concurrently. Here’s an example of how to integrate asyncio with a thread pool executor:

import asyncio
from concurrent.futures import ThreadPoolExecutor

async def main():
    with ThreadPoolExecutor() as executor:
        loop = asyncio.get_running_loop()
        result = await loop.run_in_executor(executor, your_cpu_bound_function)
        print(result)

asyncio.run(main())

This code snippet uses asyncio.run to run the main coroutine, which executes a CPU-bound function using a ThreadPoolExecutor within the asynchronous event loop framework.

Using Asyncio with Processes

To truly leverage multi-core capabilities, asyncio can be integrated with the concurrent.futures.ProcessPoolExecutor, which runs CPU-bound tasks in separate processes. The following example demonstrates combining asyncio with a process pool executor:

import asyncio
from concurrent.futures import ProcessPoolExecutor

async def main():
    with ProcessPoolExecutor() as executor:
        loop = asyncio.get_running_loop()
        result = await loop.run_in_executor(executor, your_cpu_bound_function)
        print(result)

asyncio.run(main())

This approach allows your asyncio application to run CPU-bound tasks in multiple processes, potentially on separate CPU cores, making full use of the system’s multicore architecture.

Integrating Asyncio with Multiprocessing

Python’s multiprocessing module can also be used in conjunction with asyncio to achieve multi-core parallelism. This requires a bit more setup but can offer even more control and efficiency:

import asyncio
import multiprocessing

def your_cpu_bound_function():
    # CPU-intensive computation goes here
    return "Done"

def run_in_process(async_function):
    process = multiprocessing.Process(target=async_function)
    process.start()
    process.join()

async def main():
    run_in_process(your_cpu_bound_function)

asyncio.run(main())

This approach runs the CPU-bound function in a separate process and can be used with multiple processes to utilize multiple cores effectively.

Best Practices and Caveats

While these methods can greatly enhance the execution of asyncio-based applications on multi-core systems, there are best practices and caveats to keep in mind:

  • Avoid sharing state between processes or threads, as it can lead to race conditions and make the program harder to debug.
  • Be mindful of the overhead that comes with process creation and inter-process communication.
  • Test your application’s performance with different configurations to find the optimal balance for your particular workload.

Conclusion

By integrating asyncio with multi-threading or multi-processing, Python developers can harness the full power of multi-core CPUs for their I/O-bound and CPU-bound tasks. This approach marries the simplicity and efficiency of asyncio with the raw computing power of multi-core processors. With Python 3.11, leveraging these capabilities has become more accessible and performant, empowering developers to build faster and more responsive applications.

Next Article: Python asyncio: How to download a list of files sequentially

Previous Article: Python: Convert callback-based functions to async functions

Series: Python Asynchronous Programming Tutorials

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types