Threading in Python

Jordan Woehr

August 8, 2013

Presentation at: http://pyyyc.org/threading-presentation

Introduction

Why talk about threading?

What is a thread

A sequence of program instructions managed by OS

An OS Thread

Threads vs Processes

Processes

Threads

Scheduling

OS schedules what processes/threads run

Preemption!

Modules

Python 2

Python 3

Creating threads

The class way

import threading
import time

class sleeping_thread(threading.Thread):
    def run(self):
        time.sleep(1)

t = sleeping_thread()
t.start()
t.join()

Creating threads

The function way

import threading
import time

t = threading.Thread(
    target=lambda: time.sleep(1)
)
t.start()
t.join()

Concurrency

Concurrency Primitives

Thread-safe Objects

Python Threads

What is a thread in Python?

Limitations in Python