Jason Brownlee's Blog, page 18

July 13, 2023

ThreadPoolExecutor Performance vs ThreadPool

A common justification for using the ThreadPool class over the ThreadPoolExecutor class in Python is that it is faster. We can benchmark the performance of the ThreadPoolExecutor versus the ThreadPool on common use cases of issuing one-off asynchronous tasks and batches of tasks. Comparing the benchmark results, we find very little performance difference between the […]
 •  0 comments  •  flag
Share on Twitter
Published on July 13, 2023 12:00

July 11, 2023

ThreadPoolExecutor Kill Running Tasks

You can kill running tasks in the ThreadPoolExecutor by running the ThreadPoolExecutor in a new child process and calling the terminate() or kill() method on the child process. This will abruptly and immediately terminate the child process and terminate all worker threads running in the ThreadPoolExecutor. In this tutorial, you will discover how to kill […]
 •  0 comments  •  flag
Share on Twitter
Published on July 11, 2023 12:00

July 9, 2023

ProcessPoolExecutor Max Tasks Per Child Process

You can limit the maximum tasks executed by child worker processes in the ProcessPoolExecutor via the max_tasks_per_child argument. In this tutorial, you will discover how to limit the maximum tasks per child worker process in the ProcessPoolExecutor. Let’s get started. Need to Limit Maximum Tasks Per Child Process The ProcessPoolExecutor provides a pool of reusable […]
 •  0 comments  •  flag
Share on Twitter
Published on July 09, 2023 12:00

July 6, 2023

How to Shutdown the ThreadPoolExecutor in Python

You can shutdown and close the ThreadPoolExecutor by calling the shutdown() method. You can also shutdown the ThreadPoolExecutor safely and automatically using the context manager interface or by allowing the Python interpreter to close the pool for you when exiting the Python interpreter. In this tutorial, you will discover how to shutdown the ThreadPoolExecutor in […]
 •  0 comments  •  flag
Share on Twitter
Published on July 06, 2023 12:00

July 4, 2023

ThreadPoolExecutor Dies While Running Task

Simple usage of the ThreadPoolExecutor in Python can result in unexpected behavior. Issuing a task to the thread pool that in turn issues a subsequent task can fail unexpectedly. This is not a forbidden activity, and it is not clear why this happens. In fact, this is a common “gotcha” when using the ThreadPoolExecutor, where […]
 •  0 comments  •  flag
Share on Twitter
Published on July 04, 2023 12:00

July 2, 2023

ThreadPoolExecutor Get First Result From Stream of Tasks

The ThreadPoolExecutor in Python allows one-off and batches of tasks to be issued for asynchronous execution. The API provides functions like concurrent.futures.wait() that can be used to block until one in a collection of tasks is done so that we can get the result from the first task to complete. The problem with this function […]
 •  0 comments  •  flag
Share on Twitter
Published on July 02, 2023 12:00

June 29, 2023

How to use asyncio.TaskGroup

You can manage a collection of asyncio.Task objects as a group using the asyncio.TaskGroup class. The asyncio.TaskGroup will allow tasks to be created, keep track of issued tasks, cancel all tasks if one task fails, and allow all tasks in the group to be awaited together. In this tutorial, you will discover how to use […]
 •  0 comments  •  flag
Share on Twitter
Published on June 29, 2023 12:00

June 27, 2023

How to Execute Multiple Coroutines with asyncio.Runner

You can execute multiple coroutines in the same event loop using the asyncio.Runner class in the high-level asyncio API. This is a new feature provided in Python 3.11. Before the addition of the asyncio.Runner class, we would have to execute each coroutine in a separate event loop, restructure our program to use a wrapper coroutine […]
 •  0 comments  •  flag
Share on Twitter
Published on June 27, 2023 12:00

June 25, 2023

Using Multiprocessing With Numpy Results in Worse Performance

You can parallelize numpy programs using multiprocessing. It is likely that using process-based concurrency via multiprocessing to parallelize a numpy program will result in worse overall performance. In this tutorial, you will discover the impact of using processing-based concurrency to parallelize numpy programs. Let’s get started. Should We Use Multiprocessing to Parallelize NumPy? It is […]
 •  0 comments  •  flag
Share on Twitter
Published on June 25, 2023 12:00

June 22, 2023

Using Threads With Numpy Can Result in Worse Performance

You can parallelize numpy operations in Python using threads. Many numpy operations are implemented using multithreaded algorithms, such as those that call the BLAS library. Additionally, most numpy functions release the GIL, allowing tasks that call numpy functions to be parallelized using the GIL. Nevertheless, parallelizing numpy programs using threads does not always result in […]
 •  0 comments  •  flag
Share on Twitter
Published on June 22, 2023 12:00