Introduction

Learn about the concurrency, its historical background, and methodologies to achieve it in Python.

Overview

Concurrency is the art of making a computer do (or appear to do) multiple things at once. Historically, this meant inviting the processor to switch between different tasks many times per second. In modern systems, it can also literally mean doing two or more things simultaneously on separate processor cores. Concurrency is not inherently an object-oriented topic, but Python’s concurrent systems provide object-oriented interfaces. This chapter will include the following topics:

  • Threads
  • Multiprocessing
  • Futures
  • AsyncIO
  • The dining philosophers benchmark

The case study for this chapter will address ways we can speed up model testing and hyperparameter tuning. We can’t make the computation go away, but we can leverage a modern, multi-core computer to get it done in less time.

Press + to interact
Concurrency
Concurrency

Concurrent processes can become complicated. The basic concepts are fairly simple, but the bugs that can occur are notoriously difficult to track down when the sequence of state changes is unpredictable. However, for many projects, concurrency is the only way to get the performance we need. Imagine if a web server couldn’t respond to a user’s request until another user’s request had been completed. We’ll see how to implement concurrency in Python, ...