On this page
  1. threading.Condition
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

threading.Condition

Prev: threading.BrokenBarrierError | Up: threading | Next: threading.current_thread
Rel: threading.active_count, threading.Barrier, threading.BoundedSemaphore, threading.BrokenBarrierError
Ref: docs.python.org — threading.Condition
Tags: #public #python3 #stdlib #threading


Import

import threading
from threading import Condition

Signature & Overview

threading.Condition(lock=None)

Class that implements a condition variable.

Methods & Attributes

Usage

import threading

cond = threading.Condition()
items: list[int] = []

def producer() -> None:
    with cond:
        items.append(42)
        cond.notify()

t = threading.Thread(target=producer)
t.start()
with cond:
    assert cond.wait_for(lambda: len(items) > 0, timeout=2)
    val = items.pop()
t.join()
assert val == 42

Gotchas

REPL Inspect

from threading import Condition
help(Condition)

Linked from

Related notes