Python threading Module
Example
Create and run a simple thread:
import threading
def task():
print('Thread running for Linus')
thread = threading.Thread(target=task)
print(f'Thread created: {thread.name}')
Try it Yourself »
Definition and Usage
The threading module provides a higher-level interface for working with threads in Python.
Use it to run multiple operations concurrently, synchronize threads with locks, or coordinate thread execution.
Members
Member | Description |
---|---|
Barrier | Synchronization primitive for multiple threads. |
BoundedSemaphore | Semaphore with upper limit check. |
Condition | Condition variable for thread synchronization. |
Event | Simple signaling mechanism between threads. |
Lock | Primitive lock (mutex) object. |
RLock | Reentrant lock object. |
Semaphore | Semaphore for limiting concurrent access. |
Thread | Class representing a thread of execution. |
Timer | Thread that executes a function after a delay. |
active_count() | Return number of currently active Thread objects. |
current_thread() | Return the current Thread object. |
enumerate() | Return list of all currently active Thread objects. |
main_thread() | Return the main Thread object. |