Skip to main content

Multithreading in Python

 


Learning Sections          show

Multithreading in Python

Multithreading is a programming technique used to run multiple threads (smaller units of process) concurrently within a single process. It allows for parallel execution of tasks and can significantly improve the performance of applications, especially those involving I/O-bound operations.


Creating Threads

You can create and start a new thread by using the threading module in Python:


import threading

def print_numbers():
    for i in range(10):
        print(i)

t1 = threading.Thread(target=print_numbers)
t1.start()
t1.join()  # Wait for the thread to complete

Using Thread Pools

The concurrent.futures module provides a high-level interface for asynchronously executing callables. The ThreadPoolExecutor is particularly useful for managing a pool of threads:


from concurrent.futures import ThreadPoolExecutor

def task(n):
    print(n * "Hello ")

with ThreadPoolExecutor(3) as executor:
    executor.map(task, range(5))

Thread Synchronization

Threads often need to communicate and share data. Python provides several primitives to handle synchronization, such as locks, events, conditions, and semaphores:


import threading

lock = threading.Lock()

def safe_print(msg):
    with lock:
        print(msg)

t1 = threading.Thread(target=safe_print, args=("Hello from Thread 1",))
t2 = threading.Thread(target=safe_print, args=("Hello from Thread 2",))

t1.start()
t2.start()
t1.join()
t2.join()

Using Queues

The queue module provides a thread-safe FIFO implementation that can be used to safely pass data between threads:


import queue
import threading

q = queue.Queue()

def producer():
    for i in range(5):
        q.put(i)
        print("Produced", i)

def consumer():
    while not q.empty():
        item = q.get()
        print("Consumed", item)

t1 = threading.Thread(target=producer)
t2 = threading.Thread(target=consumer)

t1.start()
t2.start()
t1.join()
t2.join()

Daemon Threads

Daemon threads run in the background and are automatically terminated when all non-daemon threads have completed. They are useful for background tasks that should not block the program from exiting:


import threading
import time

def background_task():
    while True:
        print("Running in the background")
        time.sleep(2)

t = threading.Thread(target=background_task)
t.daemon = True
t.start()

print("Main thread exiting")

Popular posts from this blog

Generators in Python

  Learning Sections          show Generators in Python Generators are a special type of iterator in Python that allow you to iterate over a sequence of items without storing them all in memory at once. They are useful for generating large sequences of data on-the-fly, or for processing data in a memory-efficient manner. Creating Generators In Python, generators are created using generator functions or generator expressions: # Generator function def my_generator ( n ): for i in range ( n ): yield i # Generator expression my_generator = ( i for i in range ( 10 )) A generator function uses the yield keyword to yield values one at a time, while a generator expression creates an anonymous generator. Iterating Over Generators You can iterate over the values produced by a generator using a for loop: for value in my_generator ( 5 ): print ( value ) This w...

Walrus Operator in Python

  Learning Sections          show Walrus Operator in Python The walrus operator ( := ) is a new assignment operator introduced in Python 3.8. It allows you to assign values to variables as part of an expression, making certain constructs more concise. Basic Usage The basic syntax for the walrus operator is: variable_name := expression Here, variable_name is assigned the value of expression , and the result of the expression is also returned. Example: Simplifying Code Consider the following example where we find and print the length of a list if it's greater than 3: # Without walrus operator my_list = [ 1 , 2 , 3 , 4 ] if len ( my_list ) > 3 : length = len ( my_list ) print ( length ) # With walrus operator my_list = [ 1 , 2 , 3 , 4 ] if ( length := len ( my_list )) > 3 : print ( length ) In the second example, the walrus operator assigns the result of len(my...

Lambda Functions in Python

  Learning Sections          show Lambda Functions in Python Lambda functions, also known as anonymous functions, are small, unnamed functions defined with the lambda keyword. They can have any number of arguments but only one expression. Lambda functions are often used for short, throwaway functions that are not needed elsewhere in the code. 1. Basic Syntax The syntax for a lambda function is: lambda arguments : expression Example: add = lambda x , y : x + y print ( add ( 2 , 3 )) # Output: 5 2. Using Lambda with Built-in Functions Lambda functions are commonly used with built-in functions like map() , filter() , and sorted() . Example with map() : numbers = [ 1 , 2 , 3 , 4 , 5 ] squared = map ( lambda x : x ** 2 , numbers ) print ( list ( squared )) # Output: [1, 4, 9, 16, 25] Example with filter() : numbers = [ 1 , 2 , 3 , 4 , 5 ] even = filter ( lambd...