Race Condition

say it: “RAYS kun-DISH-un” /ˈreɪs kənˈdɪʃən/

A bug where the result depends on which thread gets there first.

What is a race condition?

A race condition is a bug where the result depends on which of two threads gets there first, like two people buying the last ticket at the same moment. A lock makes them take turns.

import threading

tickets_left = 1
lock = threading.Lock()

def buy_ticket():
    global tickets_left
    with lock:                 # only one buyer at a time
        if tickets_left > 0:
            tickets_left -= 1

Level: Freelancer · More Python terms at this level

Updated

In other subjects