Python Guru Series ?????? - Part 4: Concurrency Programming in Python

Python Guru Series ?????? - Part 4: Concurrency Programming in Python

Hello everyone, in the previous article, we explored the Global Interpreter Lock (GIL). We discovered its limitations and its significant impact on the Python language. On the one hand, the GIL makes memory management and programming easier. On the other hand, it also affects the performance of programs, especially in tasks related to CPU-bound computations.

In today’s article, we will discuss concurrency programming in Python. We will look into various methods to achieve concurrency programming to overcome the limitations and constraints of the GIL.

Alright, let's get started.

1. Sequential Programming vs. Concurrency Programming

Before diving into the methods of concurrency programming in Python, let’s briefly go over the definition of concurrency programming.


In programming, the concepts of Sequential Programming and Concurrency Programming are quite familiar.

In Sequential programming, tasks are executed in order, one after the other. The program will not move to the next task until the current one is completed. As shown in the illustration above, you can clearly see that the program sequentially executes task 1. It waits for task 1 to finish, and then moves on to task 2.

Concurrency programming, on the other hand, involves executing tasks simultaneously. Concurrency allows multiple tasks to be performed almost at the same time. Tasks may be interleaved but do not necessarily run at the exact same moment. The program can perform multiple tasks, where each task may pause to wait for data or events to continue its execution flow. You can see the idea of concurrency programming in the illustration above.

2. Concurrency Programming in Python

Python, with the limitation of GIL, is a single-thread-based language, allowing only one thread to execute at any given time. So, what are the ways we can leverage the power of concurrency in Python? Let’s explore some popular methods below.

2.1 Threading

Like many other programming languages, we can use multi-threading to leverage the power of multithreaded programming in Python.

Multithreading is a technique that uses multiple threads running within the same process to handle various tasks. In Python, we can use threading within the threading module.

Use Case: Threading is best suited for I/O-bound tasks, such as reading/writing files, network operations, or interacting with databases.

A few points to note:

  • Python, under the influence of GIL, allows only one thread to execute at a time. This can affect the performance of CPU-bound programs, as you might have seen in the example from the previous article. However, GIL has less impact on programs that are I/O-bound.
  • I/O-bound tasks are those that rely on waiting for external resources, such as reading/writing files, accessing databases, or sending/receiving data over a network.
  • In these tasks, CPU time is not the deciding factor for performance but rather the wait time for I/O. When a task is waiting for a response from I/O (such as waiting for data from a website), the thread can temporarily release the GIL.
  • When a thread is waiting for an I/O task to complete (e.g., waiting for data from the network), Python will release the GIL, allowing other threads to execute. Therefore, although only one thread can hold the GIL at any given time, other threads can still run when the GIL is released during I/O wait times, making execution more efficient.
  • In the example below, we have multiple threads performing download_content, where each thread holds the GIL briefly to start the download, then releases it while waiting for data, and finally reacquires the GIL to process the data after the download is completed.



2.2 Multiprocessing

The method of using multithreading still has the drawback of being limited by CPU-bound tasks. We can overcome this with the multiprocessing technique in Python.

Multiprocessing involves running multiple processes, each with its own Python interpreter and memory space. This means that each process will have its own GIL. The multiprocessing module provides a way to bypass the GIL and fully utilize multiple CPU cores.

Use Case: Multiprocessing is ideal for CPU-bound tasks that require heavy computation.

A typical example of applying multiprocessing in a real-world scenario is Gunicorn when deploying a Python web application. We often initialize a number of Gunicorn process workers with configurations that match the server's CPU to handle more concurrent requests.

However, this method also reveals many limitations. Using multi-process bring higher cost of process creation compared to threading.


2.3 Asyncio

In recent years, Python 3.4 introduced Asyncio, allowing asynchronous programming in Python.

Asyncio allows you to write concurrent code using the async/await syntax, which is quite similar to JavaScript and Node.js. It is suitable for I/O-bound tasks that involve waiting, such as network calls, without the overhead of threads or processes.

Event Loop: asyncio runs on an event loop that handles asynchronous tasks. Tasks are non-blocking and can be paused and resumed, allowing other tasks to run.

Use Case: Asyncio is great for high-performance network applications, such as web servers or web scrapers.

FastAPI is a Python web framework that utilizes Asyncio to create high-performance Python web applications.


Summary

Today, we discussed the different methods of concurrency programming in Python, including threading, processing, and asyncio. Depending on the task, we can combine methods to solve our problems. Personally, I really enjoy asynchronous programming using asyncio in Python. So the next article will be on this topic. Stay tuned!

Again, I am Phan. A curious and dedicated developer. See you in the next posts.


Python Guru Series

Python Guru Series - Part 1: The Python Overview

Python Guru Series - Part 2: The Python's Interpreters

Python Guru Series - Part 3: Global Interpreter Lock (GIL)


Tài Nguy?n

?AWS Cloud - Tymer

6 个月

Interesting read! Looking forward to learning more about concurrency programming in Python.

Le The Tiem

? Software Management at GREEN VET.,JSC

7 个月

Love this

Rajashekhar Valipishetty ??

Building AlgoStraddle.com | Hiring Python Interns, Data Science Interns | Web Development Expert, Project Manager at A Plus Topper | Custom Software Solutions | 10 Yrs. Experience Finance, Education, Healthcare Consult

7 个月

Let's connect Dinh Cong Phan

Bùi Minh Hoàng

Software Engineer

7 个月

Great advice!

要查看或添加评论,请登录

Dinh Cong Phan的更多文章

社区洞察

其他会员也浏览了