O'Reilly logo
live online training icon Live Online training

Threading in Python

Programming

How to use threading to create fast and efficient applications

Lee Gaines

In this course you will learn how to make Python programs run faster by getting the most out of the operating system and its underlying components. You will learn how to create multithreaded applications that can perform multiple functions concurrently. This knowledge can help you remove bottlenecks from your system and gain skills that are valuable in building scalable applications.

In a world where speed of delivery is a high priority, it is important to be able to make your Python applications as fast and efficient as possible. One way to achieve this is by using threading to create applications that can perform more than one function at the same time. This course goes over concepts related to threading and dives into some of the builtin Python libraries that provide tools to build such applications.

What you'll learn-and how you can apply it

By the end of this live, hands-on, online course, you’ll understand:

  • What threads are and how they can be used to make applications faster
  • How to use functions from the “threading” and “concurrent” libraries in Python
  • How to prevent race conditions and other common problem when working with threads

And you’ll be able to:

  • Improve the performance of slow-running Python programs
  • Use common threading tools from the “threading” Python library
  • Create a robust and thread-safe message queuing system

This training course is for you because...

  • You’re an engineer or data scientist
  • You work with Python
  • You want to make your programs faster
  • You want to learn about threading and concurrency

Prerequisites

  • Basic programming knowledge
  • Basic understanding of an operating system
  • Beginner Python programming skills

Recommended preparation:

  • Have Python 3.4+ installed on your local machine.
  • Course repo: https://github.com/eightlimbed/python-threading
  • Have your favorite text editor and a terminal open and ready to use.
  • Code examples will be provided in a public repository for cloning and download.

Recommended follow-up:

About your instructor

  • Lee Gaines is a DevOps Engineer, certified Linux, Kubernetes, AWS, and Docker administrator, and author of Python video courses.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Overview (5 minutes)

  • Presentation: What is a thread?

A Single-Threaded Application (10 minutes)

  • Exercise: Build a single-threaded application
  • Q&A

A Multi-Threaded Application (10 minutes)

  • Exercise: Build a simple multi-threaded application
  • Q&A

Types of Threads (10 minutes)

  • Presentation: Daemon threads
  • Exercise: Convert a multi-threaded app to use daemon threads
  • Q&A

Joining Threads (10 minutes)

  • Presentation: Joining threads
  • Exercise: Use .join() to join multiple threads
  • Q&A

Multiple Threads (10 minutes)

  • Exercise: Create, start and join multiple threads using a loop
  • Q&A

ThreadPoolExecutor (10 minutes)

  • Exercise: Refactor the previous code using ThreadPoolExecutor
  • Q&A
  • Break (5 minutes)

Race Conditions (15 minutes)

  • Presentation: What is a race condition?
  • Presentation: Example of code that introduces a race condition
  • Q&A

Thread Synchronization using Locks (20 minutes)

  • Presentation: What is a Lock?
  • Presentation: Refactor race condition example to use locks
  • Presentation: What is Deadlock? (with example)
  • Q&A

Message Queue with Locks (30 minutes)

  • Presentation: What we are going to build
  • Exercise: Build a message queue system using thread objects
  • Q&A
  • Break (5 minutes)

Message Queue with the queue library (30 minutes)

  • Presentation: What we are going to build
  • Exercise: Refactor the message queue system using the queue library
  • Q&A (10 minutes)