When writing Python code that needs to perform multiple tasks at the same time, you have two main options: parallel and asynchronous programming. Both approaches allow your program to execute more than one thing simultaneously, but they work in fundamentally different ways.
Parallel Execution
Parallel programming in Python allows a program to run multiple parts of its code in parallel, leveraging multiple CPU cores on the machine. This is achieved through modules like
Here is a simple parallel program:
import multiprocessing
def print_square(num):
print(num**2)
if __name__ == "__main__":
p1 = multiprocessing.Process(target=print_square, args=(2,))
p2 = multiprocessing.Process(target=print_square, args=(3,))
p1.start()
p2.start()
p1.join()
p2.join()
This starts up two new Python processes, each running the
The key thing is that true parallel execution requires multiple Python interpreters running your code.
Asynchronous Programming
Async code runs in a single thread, but allows long-running functions to yield control back so other work can happen while waiting. This uses an event loop and callbacks. The
Here is async code that simulates two long tasks:
import asyncio
async def print_square(num):
print(f"Calculating {num**2}")
await asyncio.sleep(1)
print(f"{num**2} printed")
async def main():
await asyncio.gather(
print_square(2),
print_square(3)
)
asyncio.run(main())
This will print out both squares sequentially, but simulate 1 second of other work happening in between.
So async allows logical concurrency without true parallelism. It works great for IO-bound workloads.
The choice between parallel and async depends on your specific use case and resources available. Both are key tools for concurrent Python programming.