Concurrency Model in Python

Concurrency in Python refers to the ability of the Python interpreter to execute multiple instructions simultaneously. This is crucial in developing applications that need to handle multiple tasks or I/O-bound operations efficiently. Python has several concurrency models, each with its own strengths and weaknesses.

Python Concurrency Models

  1. Multi-threading

    • GIL (Global Interpreter Lock): Python uses a GIL which allows only one thread to execute at a time in a single process. This means that even with multiple threads, the CPU-bound tasks won't run in true parallel on multi-core processors.
    • Use Case: Ideal for I/O-bound tasks like reading from the network or waiting for user input.
  2. Multi-processing

    • No GIL: Python's multiprocessing module allows you to create multiple processes, each running in its own Python interpreter with its own GIL. This means that CPU-bound tasks can be executed in parallel.
    • Use Case: Suitable for CPU-bound tasks where the overhead of creating multiple threads is not justified.
  3. Asynchronous I/O

    • asyncio: Python's asyncio library provides a framework for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources.
    • Use Case: Ideal for I/O-bound and high-level structured network code.

Example of Asynchronous I/O

Here's an example of how to use asyncio to fetch data from multiple URLs concurrently:

import asyncio

async def fetch_url(session, url):
    async with session.get(url) as response:
        return await response.text()

async def fetch_all_urls(urls):
    async with aiohttp.ClientSession() as session:
        htmls = await asyncio.gather(
            *(fetch_url(session, url) for url in urls)
        )
        return htmls

urls = ['http://example.com', 'http://example.org', 'http://example.net']
htmls = asyncio.run(fetch_all_urls(urls))


for html in htmls:
    print(html[:100])  # Print the first 100 characters of each HTML

For more information on asyncio, you can visit the official asyncio documentation.

Concurrency Model