Loading video player…

Speed Up Python With Concurrency (Overview)

Concurrency is the act of having your computer do multiple things at the same time. If you’ve heard lots of talk about asyncio being added to Python but are curious how it compares to other concurrency methods or are wondering what concurrency is and how it might speed up your program, you’ve come to the right place.

In this course, you’ll learn the following:

  • How I/O-bound programs are affected by latency
  • Which concurrent programming patterns to use
  • What the differences are between the Python concurrency libraries
  • How to write code that uses the threading, asyncio, and multiprocessing libraries

Sample code was tested using Python 3.13. Much of the asyncio library has been in flux since Python 3.4, it is recommended to use at least Python 3.9 for the asyncio portions of the course.

Download

Sample Code (.zip)

6.4 KB
Download

Course Slides (.pdf)

1.9 MB

00:00 Welcome to Speed Up Python With Concurrency. My name is Christopher, and I will be your guide.

00:06 In this course, you’ll learn what the different types of concurrency are, how to use some of the standard libraries in Python that cover concurrency, including threading, asyncio and multiprocessing, and when to use which kinds of concurrency and when possibly to avoid it altogether.

00:25 Before I get started, a quick note on versions. This course is an update to one originally recorded in 2020. Python is always improving and new things in concurrency have come about.

00:35 The code that I demo inside of this course was tested with Python 3.14. I’ll be demoing some kinds of concurrency by fetching things from the web. For that, you’ll need requests.

00:45 I used 2.32.5 and aiohttp, where I used 3.12.15.

00:54 Speaking of hitting the web, depending on your Python installation, typically, by default, web certificates are not installed. As most websites redirect to HTTPS nowadays, you’ll need to make sure that your certs are there.

01:07 Your Python installation includes a script called Install Certificates.command, which does this installation for you.

01:15 Although you don’t have to use the version of Python, I’m using, the asyncio library in particular has changed a fair amount over the years, so if you’re using an older version of Python, you should stick with at least 3.8 if you’re playing around with asyncio.

01:31 So just what is concurrency? It’s the simple act of doing multiple things at the same time inside of your computer. For the moment, just consider a single-processor computer.

01:41 It wasn’t that long ago that these were very common in the household. It would be easy to think that a single processor computer was actually doing multiple things at a time, but it wasn’t.

01:51 This was an illusion. Think of it like a film in a film projector. The projector is showing you multiple frames per second, switching between these frames quickly, providing the illusion of motion.

02:02 A single processor computer is doing the same thing, and by switching back and forth between processes, it looks like it’s doing more than one thing at a time.

02:11 A lot of computing workloads are IO-bound. That means they’re waiting for the disk or network. Because a lot of programs spend a lot of time waiting, your operating system can take advantage of this and switch back and forth between the programs, providing the illusion that multiple programs are running simultaneously, when in reality, a single CPU can only do one thing at a time.

02:33 Of course, nowadays, multiple-processor computers have become cheap enough that they’re pretty common at home, and so you can get real concurrency by running on more than one processor at a time.

02:44 This course talks about the differences between creating concurrency in IO-bound situations versus processor-bound situations, and the techniques involved in solving both of these kinds of problems.

02:55 Python provides several mechanisms in the standard library for concurrency: threading, asyncio and multiprocessing.

03:03 threading and asyncio are two different mechanisms for handling IO-bound computing.

03:12 When solving IO-bound computing problems, you use the first two. When solving CPU-bound problems, you use the third one. One thing to be aware of when dealing with concurrency in Python is the GIL, or Global Interpreter Lock.

03:26 This is a locking mechanism that makes sure memory access is safe inside of the CPython interpreter. As of Python 3.14, work is actively ongoing to get rid of it, but the GIL is still there for now and can get in your way.

03:40 To delve into IO-bound concurrency, you first need to understand latency in processing, so I’ll be talking about that next.

Become a Member to join the conversation.