Python Multithreading#

Learning Objectives#

By the end of this lesson, learners will be able to:

  • Use Python’s multithreading library to parallelize an IO bound problem.

  • Set up a pool of worker threads and delegate tasks to different threads to run concurrently, , using the ThreadPoolExecutor class.

IO bound example with Python multithreading#

In this simple example we will download a list of webpages, and report the amount of time it took to run for benchmarking purposes. First we will do it serially, and then use the multithreading library to delegate blocks of webpages to different threads. This problem is IO bound, as the time it takes for each download (i.e. a task) is network speed dependent, which can be variable.

The serial example#

Download complete serial io bound example file

The multithreading example#

Download complete threaded io_bound example file

from jupyterquiz import display_quiz
display_quiz("questions/summary_multithreading.json")