### Complete Example: Extending Thread Class in Python Source: https://superfastpython.com/threading-in-python/index This is a complete example demonstrating how to extend the `threading.Thread` class in Python. It includes defining a custom thread, overriding `run()`, starting the thread, and waiting for it to finish. ```python # SuperFastPython.com # example of extending the Thread class from time import sleep from threading import Thread # custom thread class class CustomThread(Thread): # override the run function def run(self): # block for a moment sleep(1) # display a message print('This is coming from another thread') # create the thread thread = CustomThread() # start the thread thread.start() # wait for the thread to finish print('Waiting for the thread to finish') thread.join() ``` -------------------------------- ### Python Livelock Demonstration with Threads and Locks Source: https://superfastpython.com/threading-in-python/index Sets up and runs a Python livelock example. It creates two mutex locks and two threads, each executing the 'task' function with the locks provided in a different order to induce a livelock. The main thread then starts the worker threads and waits for them to complete. ```python from threading import Thread, Lock from time import sleep def task(number, lock1, lock2): while True: with lock1: sleep(0.1) if lock2.locked(): print(f'Task {number} cannot get the second lock, giving up...') else: with lock2: print(f'Task {number} made it, all done.') break # create locks lock1 = Lock() lock2 = Lock() # create threads thread1 = Thread(target=task, args=(0, lock1, lock2)) thread2 = Thread(target=task, args=(1, lock2, lock1)) # start threads thread1.start() thread2.start() # wait for threads to finish thread1.join() thread2.join() ``` -------------------------------- ### Complete Python Example: Wait and Notify with Condition Source: https://superfastpython.com/threading-in-python/index A full example demonstrating inter-thread communication using threading.Condition. It includes the worker thread's task function and the main thread's logic for creating, starting, waiting, and finally processing data. This example highlights the importance of acquiring the condition's lock before starting the worker thread to prevent race conditions. ```python # SuperFastPython.com # example of wait/notify with a condition from time import sleep from threading import Thread from threading import Condition # target function to prepare some work def task(condition, work_list): # block for a moment sleep(1) # add data to the work list work_list.append(33) # notify a waiting thread that the work is done print('Thread sending notification...') with condition: condition.notify() # create a condition condition = Condition() # prepare the work list work_list = list() # wait to be notified that the data is ready print('Main thread waiting for data...') with condition: # start a new thread to perform some work worker = Thread(target=task, args=(condition, work_list)) worker.start() # wait to be notified condition.wait() # we know the data is ready print(f'Got data: {work_list}') ``` -------------------------------- ### Complete Example: Stopping a Thread Gracefully in Python Source: https://superfastpython.com/threading-in-python/index A comprehensive example demonstrating how to create a thread, run a task within it that checks for a stop signal (threading.Event), and how the main thread signals and waits for the worker thread to terminate. ```python # SuperFastPython.com # example of stopping a new thread from time import sleep from threading import Thread from threading import Event # custom task function def task(event): # execute a task in a loop for i in range(5): # block for a moment sleep(1) # check for stop if event.is_set(): break # report a message print('Worker thread running...') print('Worker closing down') # create the event event = Event() # create and configure a new thread thread = Thread(target=task, args=(event,)) # start the new thread thread.start() # block for a while sleep(3) # stop the worker thread print('Main stopping thread') event.set() # wait for the new thread to finish thread.join() ``` -------------------------------- ### Create and Start a Custom Thread in Python Source: https://superfastpython.com/threading-in-python/index This snippet shows how to create an instance of a custom thread class and start its execution using the `start()` method. The `start()` method internally calls the `run()` method in a new thread. ```python # create the thread thread = CustomThread() # start the thread thread.start() ``` -------------------------------- ### Get Main Thread Instance using main_thread() in Python Source: https://superfastpython.com/threading-in-python/index Shows how to retrieve a threading.Thread instance for the main thread from any thread using the threading.main_thread() function. The example reports the name, daemon status, and identifier of the main thread. ```python # SuperFastPython.com # example of getting the main thread from threading import main_thread # get the main thread thread = main_thread() # report properties for the main thread print(f'name={thread.name}, daemon={thread.daemon}, id={thread.ident}') ``` -------------------------------- ### Configure Daemon Thread via Property in Python Source: https://superfastpython.com/threading-in-python/index This example shows how to create a standard threading.Thread instance and then configure it to be a daemon thread by setting its 'daemon' property to True after instantiation. Finally, it prints the daemon status. ```python # SuperFastPython.com # example of setting a thread to be a daemon via the property from threading import Thread # create a thread thread = Thread() # configure the thread to be a daemon thread.daemon = True # report if the thread is a daemon print(thread.daemon) ``` -------------------------------- ### Attempt to Restart a Terminated Thread in Python Source: https://superfastpython.com/threading-in-python/index Illustrates the behavior when attempting to restart a thread after it has terminated. This example shows that calling start() on a terminated thread raises a RuntimeError, as threads can only be started once. ```python # SuperFastPython.com # example of reusing a thread from time import sleep from threading import Thread # custom target function def task(): # block for a moment sleep(1) # report a message print('Hello from the new thread') # create a new thread thread = Thread(target=task) # start the thread thread.start() # wait for the thread to finish thread.join() # try and start the thread again thread.start() ``` -------------------------------- ### Run a Function in a New Thread in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to create and run a simple function in a new thread. It defines a task function, creates a `threading.Thread` instance targeting that function, starts the thread, and then waits for it to complete using `join()`. ```python # SuperFastPython.com # example of running a function in another thread from time import sleep from threading import Thread # a custom function that blocks for a moment def task(): # block for a moment sleep(1) # display a message print('This is from another thread') # create a thread thread = Thread(target=task) # run the thread thread.start() # wait for the thread to finish print('Waiting for the thread...') thread.join() ``` -------------------------------- ### Set Thread Name via Constructor (Python) Source: https://superfastpython.com/threading-in-python/index Shows how to assign a custom name to a `threading.Thread` instance during its creation using the 'name' argument. The example then prints the assigned name. ```python # SuperFastPython.com # example of setting the thread name in the constructor from threading import Thread # create a thread with a custom name thread = Thread(name='MyThread') # report thread name print(thread.name) ``` -------------------------------- ### Create Daemon Thread via Constructor in Python Source: https://superfastpython.com/threading-in-python/index This example demonstrates how to create a new thread and configure it to be a daemon thread directly within the threading.Thread constructor by setting the 'daemon' argument to True. It then prints the daemon status of the thread. ```python # SuperFastPython.com # example of setting a thread to be a daemon via the constructor from threading import Thread # create a daemon thread thread = Thread(daemon=True) # report if the thread is a daemon print(thread.daemon) ``` -------------------------------- ### Get Current Thread Instance using current_thread() in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to obtain a threading.Thread instance for the currently executing thread using threading.current_thread(). The example defines a task function that reports its own thread's name. ```python # SuperFastPython.com # retrieve the current thread within from threading import Thread from threading import current_thread # function to get the current thread def task(): # get the current thread thread = current_thread() # report the name print(thread.name) # create a thread thread = Thread(target=task) # start the thread thread.start() # wait for the thread to exit thread.join() ``` -------------------------------- ### Get Main Thread Instance using current_thread() in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to obtain a threading.Thread instance representing the main thread by calling threading.current_thread(). It then reports properties like name, daemon status, and identifier. ```python # SuperFastPython.com # example of getting the current thread for the main thread from threading import current_thread # get the main thread thread = current_thread() # report properties for the main thread print(f'name={thread.name}, daemon={thread.daemon}, id={thread.ident}') ``` -------------------------------- ### Complete Reentrant Lock Example in Python Source: https://superfastpython.com/threading-in-python/index A full example showcasing the use of threading.RLock in Python. It defines functions for reporting and task execution, both protected by the same RLock. This demonstrates how a thread holding the lock can re-enter it, preventing deadlocks. ```python # SuperFastPython.com # example of a reentrant lock from time import sleep from random import random from threading import Thread from threading import RLock # reporting function def report(lock, identifier): # acquire the lock with lock: with lock: print(f'>thread {identifier} done') # work function def task(lock, identifier, value): # acquire the lock with lock: with lock: print(f'>thread {identifier} sleeping for {value}') sleep(value) # report report(lock, identifier) # create a shared reentrant lock lock = RLock() # start a few threads that attempt to execute the same critical section for i in range(10): # start a thread Thread(target=task, args=(lock, i, random())).start() # wait for all threads to finish... # (In a real application, you'd likely use thread.join() or similar) ``` -------------------------------- ### Set Thread Name via Property (Python) Source: https://superfastpython.com/threading-in-python/index Demonstrates how to set the name of an existing `threading.Thread` instance by assigning a value to its 'name' property after creation. The example then prints the updated name. ```python # SuperFastPython.com # example of setting the thread name via the property from threading import Thread # create a thread thread = Thread() # set the name thread.name = 'MyThread' # report thread name print(thread.name) ``` -------------------------------- ### Python Thread Barrier Example Source: https://superfastpython.com/threading-in-python/index Demonstrates using `threading.Barrier` to synchronize multiple worker threads and the main thread. Each thread performs a task, waits at the barrier, and the main thread waits for all workers to complete. ```python # SuperFastPython.com # example of using a barrier from time import sleep from random import random from threading import Thread from threading import Barrier # target function to prepare some work def task(barrier, number): # generate a unique value value = random() * 10 # block for a moment sleep(value) # report result print(f'Thread {number} done, got: {value}') # wait on all other threads to complete barrier.wait() # create a barrier barrier = Barrier(5 + 1) # create the worker threads for i in range(5): # start a new thread to perform some work worker = Thread(target=task, args=(barrier, i)) worker.start() # wait for all threads to finish print('Main thread waiting on all results...') barrier.wait() # report once all threads are done print('All threads have their result') ``` -------------------------------- ### Wait on Barrier in Python Source: https://superfastpython.com/threading-in-python/index Provides an example of a thread waiting at the barrier. The wait() function blocks until all specified parties arrive. It returns the number of remaining parties. ```python # wait on the barrier for all other threads to arrive barrier.wait() ``` -------------------------------- ### Python: Create and Use Semaphore with Threads Source: https://superfastpython.com/threading-in-python/index Illustrates the complete example of using a semaphore in Python for thread synchronization. It initializes a semaphore with a limit, creates multiple worker threads that execute a task function, and ensures all threads complete before the main program exits. The semaphore limits concurrent access to the task. ```python # SuperFastPython.com # example of using a semaphore from time import sleep from random import random from threading import Thread from threading import Semaphore # target function def task(semaphore, number): # attempt to acquire the semaphore with semaphore: # process value = random() sleep(value) # report result print(f'Thread {number} got {value}') # create a semaphoresemaphore = Semaphore(2) # create a suite of threads for i in range(10): worker = Thread(target=task, args=(semaphore, i)) worker.start() # wait for all workers to complete... ``` -------------------------------- ### Schedule Function Execution with threading.Timer in Python Source: https://superfastpython.com/threading-in-python/index Illustrates how to use the threading.Timer class to execute a function after a specified delay. The Timer is a subclass of Thread and can be started, and optionally cancelled, before the delay elapses. ```python # configure a timer thread timer = Timer(10, task, args=(arg1, arg2)) ``` ```python # start the timer thread timer.start() ``` ```python # cancel the timer thread timer.cancel() ``` -------------------------------- ### Python Blocking Lock Acquisition Source: https://superfastpython.com/threading-in-python/index This example shows the standard way to acquire a lock in Python by blocking. The thread will pause execution until the lock becomes available, unlike a spinlock which continuously checks. This is generally preferred to avoid busy-waiting. ```python # block until the lock can be acquired lock.acquire() ``` -------------------------------- ### Get and Set Python Context Switch Interval Source: https://superfastpython.com/threading-in-python/index Demonstrates how to retrieve the current context switch interval and set a new interval in seconds using Python's sys module. This affects how long a Python thread runs before being considered for a context switch. ```python # Get the current switch interval interval = sys.getswitchinterval() print(f"Current switch interval: {interval} seconds") # Set a new switch interval (e.g., 1.0 second) sys.setswitchinterval(1.0) print(f"New switch interval set to: {sys.getswitchinterval()} seconds") ``` -------------------------------- ### Handling delays before condition.wait() in Python Source: https://superfastpython.com/threading-in-python/index This example demonstrates how a thread can set an event, introduce a delay, and then wait on a condition. Because the condition is acquired before waiting, the main thread cannot call notify() until this thread calls wait(), preventing race conditions. ```python with condition: event.set() sleep(5) condition.wait() ``` -------------------------------- ### Handle Unhandled Exceptions in Python Threads Source: https://superfastpython.com/threading-in-python/index Demonstrates how an unhandled exception in a thread unwinds the thread and reports the error to standard error without affecting the main thread. The example defines a target function that raises an exception after a delay, creates a thread to run it, and then joins the thread. ```python # SuperFastPython.com # example of an unhandled exception in a thread from time import sleep from threading import Thread # target function that raises an exception def work(): print('Working...') sleep(1) # rise an exception raise Exception('Something bad happened') # create a thread thread = Thread(target=work) # run the thread thread.start() # wait for the thread to finish thread.join() # continue on print('Continuing on...') ``` -------------------------------- ### Get Thread Identifier in Python Source: https://superfastpython.com/threading-in-python/index Shows how to access the 'ident' property of a threading.Thread instance to get its unique Python interpreter-assigned identifier. The identifier is assigned after the thread starts. ```python # SuperFastPython.com # example of reporting the thread identifier from threading import Thread # create the thread thread = Thread() # report the thread identifier print(thread.ident) # start the thread thread.start() # report the thread identifier print(thread.ident) ``` -------------------------------- ### Configure Barrier with Action in Python Source: https://superfastpython.com/threading-in-python/index Shows how to configure a barrier to execute a specific action once all threads have arrived. The 'action' argument takes a callable (like a function or lambda) that runs before threads are released. ```python # configure a barrier with an action barrier = threading.Barrier(10, action=my_function) ``` -------------------------------- ### Get Thread Native Identifier in Python Source: https://superfastpython.com/threading-in-python/index Illustrates how to retrieve the 'native_id' property of a threading.Thread instance, which represents the operating system-assigned unique identifier for the thread. This ID is available after the thread has started. ```python # SuperFastPython.com # example of reporting the native thread identifier from threading import Thread # create the thread thread = Thread() # report the native thread identifier print(thread.native_id) # start the thread thread.start() # report the native thread identifier print(thread.native_id) ``` -------------------------------- ### Using Countdown Latch in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates the usage of the CountDownLatch class. It shows how to initialize the latch with a specific count and how to use the count_down() and wait() methods to coordinate threads. ```python # create the countdown latch latch = CountDownLatch(5) # ... other thread operations ... # As each party arrives at the latch, call count_down() latch.count_down() # Any threads interested in when the latch is open can call wait() latch.wait() ``` -------------------------------- ### Get Number of Active Threads using active_count() in Python Source: https://superfastpython.com/threading-in-python/index Illustrates how to find the number of currently active (alive) threads in a Python process using the threading.active_count() function. The example prints the integer count returned by the function. ```python # SuperFastPython.com # report the number of active threads from threading import active_count # get the number of active threads count = active_count() # report the number of active threads print(count) ``` -------------------------------- ### Create a Barrier in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates the basic creation of a threading.Barrier instance. The barrier is initialized with the number of threads (parties) that must reach it before it can be passed. ```python # create a barrier barrier = threading.Barrier(10) ``` -------------------------------- ### Python Main Thread Waiting for Condition Notification Source: https://superfastpython.com/threading-in-python/index Sets up the main thread to wait for a notification. It initializes a Condition object and a shared list, starts a worker thread, and then waits using condition.wait() until the worker thread signals completion. It requires the Condition and list to be initialized prior to starting the worker thread. ```python # create a condition condition = Condition() # prepare the work list work_list = list() # wait to be notified that the data is ready print('Main thread waiting for data...') with condition: # start a new thread to perform some work worker = Thread(target=task, args=(condition, work_list)) worker.start() # wait to be notified condition.wait() ``` -------------------------------- ### Manual Lock Acquisition and Release in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates the traditional method of manually acquiring and releasing a lock using acquire() and release() methods. This approach requires careful management within try-finally blocks to ensure the lock is always released, even if errors occur. ```python import threading lock = threading.Lock() # acquire the lock manually lock.acquire() # critical section... # release the lock lock.release() ``` -------------------------------- ### Get the Name of a Thread in Python Source: https://superfastpython.com/threading-in-python/index Shows how to retrieve the name of a thread instance using the 'name' property. Thread names default to 'Thread-N' if not explicitly set. ```python # report the name of the thread print(thread.name) ``` -------------------------------- ### Create a threading.Event Object in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to create an instance of the threading.Event object. The event is initially in the 'not set' state (False). This is the first step before any communication can occur. ```python import threading # create an instance of an event event = threading.Event() ``` -------------------------------- ### Override the run() Method for Thread Execution in Python Source: https://superfastpython.com/threading-in-python/index This snippet demonstrates overriding the `run()` method of a custom thread class. The code within this method will be executed when the thread is started. ```python # override the run function def run(self): # ... ``` -------------------------------- ### Create a Thread with a Custom Name in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to assign a custom name to a thread during its creation using the 'name' argument in the threading.Thread constructor. ```python # create with a custom name thread = Thread(name='MyThread') ``` -------------------------------- ### Acquiring Lock with Timeout in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to acquire a lock with a specified timeout in Python. This prevents threads from blocking indefinitely and allows for handling cases where the lock cannot be acquired within the given time. ```python import threading lock = threading.Lock() # acquire the lock with a timeout of 2 minutes if not lock.acquire(timeout=2*60): # handle failure case, e.g., report error, force termination ``` -------------------------------- ### Configure Barrier with Timeout in Python Source: https://superfastpython.com/threading-in-python/index Illustrates setting a default timeout for threads waiting on the barrier. If the timeout expires before all threads arrive, a BrokenBarrierError is raised. ```python # configure a barrier with a default timeout barrier = threading.Barrier(10, timeout=5) ``` -------------------------------- ### Check if a Thread is Alive (Python) Source: https://superfastpython.com/threading-in-python/index Demonstrates how to check if a `threading.Thread` instance is alive using the `is_alive()` method. Initially, a thread is not alive. After starting, it becomes alive until its execution completes. The `join()` method can be used to wait for thread completion. ```python # SuperFastPython.com # example of assessing whether a thread is alive from threading import Thread # create the thread thread = Thread() # report the thread is alive print(thread.is_alive()) ``` ```python # SuperFastPython.com # example of assessing whether a running thread is alive from time import sleep from threading import Thread # create the thread thread = Thread(target=lambda:sleep(1)) # report the thread is alive print(thread.is_alive()) # start the thread thread.start() # report the thread is alive print(thread.is_alive()) # wait for the thread to finish thread.join() # report the thread is alive print(thread.is_alive()) ``` -------------------------------- ### Context Manager for Lock Acquisition in Python Source: https://superfastpython.com/threading-in-python/index Shows the recommended and concise way to manage locks in Python using the `with` statement (context manager). This automatically handles acquiring and releasing the lock, ensuring it's always released upon exiting the block. ```python import threading lock = threading.Lock() # acquire the lock using a context manager with lock: # critical section... ``` -------------------------------- ### Get Native Thread Identifier using threading.get_native_id() Source: https://superfastpython.com/threading-in-python/index Retrieve the native thread identifier assigned by the operating system for the current thread. This is achieved using the `threading.get_native_id()` function. Similar to the Python identifier, the native ID will vary with each execution. ```python # SuperFastPython.com # report the native id for the current thread from threading import get_native_id # get the native id for the current thread identifier = get_native_id() # report the thread id print(identifier) ``` -------------------------------- ### Using Try-Finally for Lock Management in Python Source: https://superfastpython.com/threading-in-python/index Illustrates a more robust, though verbose, method for managing locks using a try-finally block. This ensures that the lock is released even if exceptions are raised within the critical section. ```python import threading lock = threading.Lock() # acquire the lock lock.acquire() try: # critical section... finally: # always release the lock lock.release() ``` -------------------------------- ### Get Python Thread Identifier using threading.get_ident() Source: https://superfastpython.com/threading-in-python/index Retrieve the unique Python thread identifier for the current thread. This identifier is assigned by the Python interpreter and can be obtained using the `threading.get_ident()` function. The identifier will differ each time the code is run. ```python # SuperFastPython.com # report the id for the current thread from threading import get_ident # get the id for the current thread identifier = get_ident() # report the thread id print(identifier) ``` -------------------------------- ### Join a Thread in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to wait for a thread to complete its execution using the Thread.join() method. This blocks the current thread until the target thread terminates. ```python # join a thread thread.join() ``` -------------------------------- ### Initialize a Lock Object in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates the basic step of creating an instance of a lock object in Python using the threading.Lock class. This lock will be used to synchronize access to shared resources, such as a counter, across multiple threads. ```python # create a lock lock = Lock() ``` -------------------------------- ### Implement Countdown Latch in Python Source: https://superfastpython.com/threading-in-python/index A custom implementation of a countdown latch using Python's threading.Condition. It allows threads to wait until a specified count reaches zero. The latch starts closed and opens once the count is reached, notifying all waiting threads. ```python from threading import Condition class CountDownLatch(): def __init__(self, count): self.count = count self.condition = Condition() def count_down(self): with self.condition: if self.count == 0: return self.count -= 1 if self.count == 0: self.condition.notify_all() def wait(self): with self.condition: if self.count == 0: return self.condition.wait() ``` -------------------------------- ### Enumerate Active Threads using threading.enumerate() Source: https://superfastpython.com/threading-in-python/index Obtain a list of all currently active (alive) threads within a Python process using `threading.enumerate()`. The list always includes the main thread. The example iterates through the list and prints the name of each active thread. ```python # SuperFastPython.com # enumerate all active threads import threading # get a list of all active threads threads = threading.enumerate() # report the name of all active threads for thread in threads: print(thread.name) ``` -------------------------------- ### Use Context Manager for Thread Condition in Python Source: https://superfastpython.com/threading-in-python/index Illustrates using the 'with' statement (context manager) for acquiring and releasing a threading.Condition object. This simplifies the code by automatically handling the acquire and release operations, making it safer and more readable. ```python # acquire the condition with condition: # wait to be notified condition.wait() ``` -------------------------------- ### Query Default Thread Name in Python Source: https://superfastpython.com/threading-in-python/index Shows how to access the default name assigned to a `threading.Thread` instance. Threads are automatically named in a unique manner within each process, typically in the format 'Thread-%d'. This example creates a thread and prints its default name. ```python # SuperFastPython.com # example of accessing the thread name from threading import Thread # create the thread thread = Thread() # report the thread name print(thread.name) ``` -------------------------------- ### Execute a Command and Capture Output in Python Source: https://superfastpython.com/threading-in-python/index This snippet demonstrates how to execute an external command using `subprocess.Popen` in Python and capture its standard output and standard error. It's useful for running shell commands and processing their results within a Python script. Note that `shell=True` can be a security risk if the command is not trusted. ```python import subprocess my_cmd = 'nohup ./your_executable_name &' result = subprocess.Popen(my_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = result.communicate() ``` -------------------------------- ### Use Context Manager for Lock Protection in Python Source: https://superfastpython.com/threading-in-python/index Shows a more concise way to manage locks in Python using the 'with' statement (context manager). This approach automatically acquires the lock before entering the block and releases it upon exiting, simplifying thread-safe code and reducing the risk of forgetting to release the lock. ```python # acquire the lock protecting the counter with lock: # update the counter counter += 1 ``` -------------------------------- ### Python Spinlock with Timeout Source: https://superfastpython.com/threading-in-python/index This Python code demonstrates a spinlock using a timeout for acquiring the lock. The `acquire(timeout=0.1)` method attempts to get the lock for a specified duration, preventing indefinite busy-waiting and allowing the thread to proceed if the lock isn't acquired within the timeout. ```python while True: # try and get the lock with a timeout acquired = lock.acquire(timeout=0.1) # check if we got the lock if acquired: # stop waiting break ``` -------------------------------- ### Python Mutex Lock Example Source: https://superfastpython.com/threading-in-python/index Demonstrates using a threading.Lock to protect a critical section in a multithreaded Python application. The lock ensures that only one thread can access the shared resource (printing and sleeping) at a time, preventing race conditions. It requires the 'threading' and 'time' modules. ```python # SuperFastPython.com # example of a mutual exclusion (mutex) lock from time import sleep from random import random from threading import Thread from threading import Lock # work function def task(lock, identifier, value): # acquire the lock with lock: print(f'>thread {identifier} got the lock, sleeping for {value}') sleep(value) # create a shared lock lock = Lock() # start a few threads that attempt to execute the same critical section for i in range(10): # start a thread Thread(target=task, args=(lock, i, random())).start() # wait for all threads to finish... ``` -------------------------------- ### Waiting for Thread Results in Python Source: https://superfastpython.com/threading-in-python/index Explores common strategies for retrieving results from a new thread in Python. It outlines indirect methods like using instance variables, queues, or global variables, as direct result retrieval is not supported. ```python # Method 1: Use instance variables on an extended threading.Thread # Method 2: Use a queue.Queue to share a result between threads # Method 3: Use a global variable to share a result between threads ``` -------------------------------- ### Python Task Function for Livelock Example Source: https://superfastpython.com/threading-in-python/index Defines a task function for worker threads that attempts to acquire two locks in a specific order. If the second lock is unavailable, it releases the first lock and retries, potentially leading to a livelock. It requires the 'threading' module for locks and the 'time' module for sleeping. ```python from threading import Lock from time import sleep def task(number, lock1, lock2): # loop until the task is completed while True: # acquire the first lock with lock1: # wait a moment sleep(0.1) # check if the second lock is available if lock2.locked(): print(f'Task {number} cannot get the second lock, giving up...') else: # acquire lock2 with lock2: print(f'Task {number} made it, all done.') break ``` -------------------------------- ### Reset Barrier in Python Source: https://superfastpython.com/threading-in-python/index Demonstrates how to reset a broken barrier. The reset() function makes the barrier usable again, allowing threads to synchronize on it. ```python # reset a broken barrier barrier.reset() ``` -------------------------------- ### Python: Thread-Local Data Storage with threading.local Source: https://superfastpython.com/threading-in-python/index Illustrates thread-local data storage using the threading.local class. Each thread can store and access its own data using properties on a local instance, preventing data conflicts between threads. This is achieved by creating a threading.local() instance and assigning attributes to it within each thread. ```python # create a local instance local = threading.local() # store some data local.custom = 33 ``` ```python # SuperFastPython.com # example of thread local storage from time import sleep import threading # custom target function def task(value): # create a local storage local = threading.local() # store data local.value = value # block for a moment sleep(value) # retrieve value print(f'Stored value: {local.value}') # create and start a thread threading.Thread(target=task, args=(1,)).start() # create and start another thread threading.Thread(target=task, args=(2,)).start() ```