Category: 1. Python Concurrency

  • Python asyncio.gather()

    Summary: in this tutorial, you’ll learn how to use the Python asyncio.gather() function to run multiple asynchronous operations. Introduction to the Python asyncio.gather() function Sometimes, you may want to run multiple asynchronous operations and get the results once they are complete. To do that you can use the asyncio.gather() function: The asyncio.gather() function has two parameters: The asyncio.gather() returns the results of awaitables…

  • Python asyncio Future

    Summary: in this tutorial, you’ll learn about Python asyncio future objects and understand how they work. Introduction to the Python asyncio future A future is an object that returns a value later in the future but not now. Typically, a future object is the result of an asynchronous operation. For example, you may call an API from…

  • Python asyncio.wait()

    Summary: in this tutorial, you’ll learn about the asyncio.wait() function to run an iterable of awaitable objects concurrently. Introduction to the Python asyncio wait() function The asyncio.wait() function runs an iterable of awaitables objects and blocks until a specified condition. Here’s the syntax of the asyncio.wait() function: The asyncio.wait() function has the following parameters: Constant Description FIRST_COMPLETED Return when all awaitables are complete…

  • Python asyncio.wait_for()

    Summary: in this tutorial, you’ll learn how to use the asyncio.wait_for() function to wait for a coroutine to complete with a timeout. Introduction to the Python asyncio.wait_for() function In the previous tutorial, you learned how to cancel a task that is in progress by using the cancel() method of the Task object. To wait for a task to complete with a…

  • Cancelling Tasks

    Summary: in this tutorial, you’ll learn how to cancel a long-running asynchronous operation that may take forever to complete. The following statement uses the await statement to wait for a task to be complete: However, if the coroutine() took forever, you would be stuck waiting for the await statement to finish without obtaining any result. Additionally, you would have no way to stop…

  • Python asyncio.create_task()

    Summary: in this tutorial, you’ll learn how to use asyncio.create_task() function to run multiple tasks concurrently. Simulating a long-running operation To simulate a long-running operation, you can use the sleep() coroutine of the asyncio package. The sleep() function delays a specified number of the second: Because sleep() is a coroutine, you need to use the await keyword. For example, the following uses the sleep() coroutine to simulate an API…

  • Python async/await

    Summary: in this tutorial, you will learn about Python coroutines and how to use the Python async and await keywords to create and pause coroutines. Introduction to Python coroutines A coroutine is a regular function with the ability to pause its execution when encountering an operation that may take a while to complete. When the long-running operation completes, you can resume…

  • Python Event Loop

    Summary: in this tutorial, you’ll learn about the Python event loop and how Python uses it to achieve the concurrency model using a single thread. Introduction to the Python event loop Concurrency means multiple tasks can run at the same time. The asyncio built-in package allows you to run tasks concurrently using a single thread. To achieve a single-threaded concurrency…

  • Python ProcessPoolExecutor

    Summary: in this tutorial, you’ll learn how to use the Python ProcessPoolExecutor to create and manage a process pool effectively. Introduction to the Python ProcessPoolExecutor class In the previous tutorial, you learned how to run code in parallel by creating processes manually using the Process class from the multiprocessing module. However, manually creating processes is not efficient. To manage the processes…

  • Python Multiprocessing

    Summary: in this tutorial, you’ll learn how to run code in parallel using the Python multiprocessing module. Introduction to the Python multiprocessing Generally, programs deal with two types of tasks: Multiprocessing allows two or more processors to simultaneously process two or more different parts of a program. In Python, you use the multiprocessing module to implement multiprocessing.…