Category: 1. Python Concurrency

  • Python Thread-safe Queue

    Summary: in this tutorial, you’ll learn how to use a Python thread-safe queue to exchange data safely between multiple threads. Introduction to the Python thread-safe queue The built-in queue module allows you to exchange data safely between multiple threads. The Queue class in the queue module implements all required locking semantics. Creating a new queue To create a new queue, you import…

  • Python Semaphore

    Summary: in this tutorial, you will learn how to use Python Semaphore to control the number of threads that can access a shared resource simultaneously. Introduction to the Python Semaphore A Python semaphore is a synchronization primitive that allows you to control access to a shared resource. Basically, a semaphore is a counter associated with…

  • How to Stop a Thread in Python

    Summary: in this tutorial, you’ll learn how to stop a thread in Python from the main thread using the Event class of the threading module. Introduction to the Event object To stop a thread, you use the Event class of the threading module. The Event class has an internal thread-safe boolean flag that can be set to True or False. By default, the internal flag…

  • Python Threading Event

    Summary: in this tutorial, you’ll learn how to use the Python threading Event object to communicate between threads. Introduction to the Python threading Event object Sometimes, you need to communicate between the threads. To do that, you can use a lock (mutex) and a boolean variable. However, Python provides you with a better way to communicate between threads…

  • How to use the Python Threading Lock to Prevent Race Conditions

    Summary: in this tutorial, you’ll learn about race conditions and how to use the Python threading Lock object to prevent them. What is a race condition A race condition occurs when two or more threads try to access a shared variable simultaneously, leading to unpredictable outcomes. In this scenario, the first thread reads the value from the…

  • Python ThreadPoolExecutor

    Summary: in this tutorial, you’ll learn how to use the Python ThreadPoolExecutor to develop multi-threaded programs. Introduction to the Python ThreadPoolExecutor class In the multithreading tutorial, you learned how to manage multiple threads in a program using the Thread class of the threading module. The Thread class is useful when you want to create threads manually. However, manually managing threads is not efficient…

  • Python Daemon Threads

    Summary: in this tutorial, you’ll learn about Python daemon threads and how to use them effectively. Introduction to the Python daemon threads In Python, every program has at least one thread called the main thread. To create a program that has more than one thread, you use the threading module. By using multiple threads, you can execute tasks…

  • Python Multithreading Example

    Summary: in this tutorial, you’ll learn how to use the Python threading module to develop a multithreaded program. Extending the Thread class We’ll develop a multithreaded program that scraps the stock prices from the Yahoo Finance website. To do that, we’ll use two third-party packages: First, install the requests and lxml modules using the pip command: Next, define a new class…

  • How to Return Values from a Thread

    Summary: in this tutorial, you will learn how to return values from a child thread to the main thread by extending the threading.Thread class. When a Python program starts, it has a single thread called the main thread. Sometimes, you want to offload I/O tasks to new threads and execute them concurrently. Additionally, you may also want to get the…

  • How to Extend Python Thread Class

    Summary: in this tutorial, you will learn how to extend the Python Thread class to run code in a new thread. Introduction to Python Thread Class When a Python program starts, it has a thread called the main thread. Sometimes, you want to offload the I/O-bound tasks to a new thread to execute them concurrently. To do that,…