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:
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
?AWS Cloud - Tymer
6 个月Interesting read! Looking forward to learning more about concurrency programming in Python.
? Software Management at GREEN VET.,JSC
7 个月Love this
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
Software Engineer
7 个月Great advice!