Multiprocessing Share Unserializable Objects Between Processes

Most of the time it’s not really desirable to pass the reference of an existing object to another process. Instead you create your class you want to share between processes: class MySharedClass: # stuff… Then you make a proxy manager like this: import multiprocessing.managers as m class MyManager(m.BaseManager): pass # Pass is really enough. Nothing … Read more

Python: concurrent.futures How to make it cancelable?

Unfortunately, running Futures cannot be cancelled. I believe the core reason is to ensure the same API over different implementations (it’s not possible to interrupt running threads or coroutines). The Pebble library was designed to overcome this and other limitations. from pebble import ProcessPool def function(foo, bar=0): return foo + bar with ProcessPool() as pool: … Read more

Use tqdm with concurrent.futures?

You can wrap tqdm around the executor as the following to track the progress: list(tqdm(executor.map(f, iter), total=len(iter)) Here is your example: import time import concurrent.futures from tqdm import tqdm def f(x): time.sleep(0.001) # to visualize the progress return x**2 def run(f, my_iter): with concurrent.futures.ThreadPoolExecutor() as executor: results = list(tqdm(executor.map(f, my_iter), total=len(my_iter))) return results my_iter = … Read more

Is having a concurrent.futures.ThreadPoolExecutor call dangerous in a FastAPI endpoint?

You should rather use the HTTPX library, which provides an async API. As described in this answer , you spawn a Client and reuse it every time you need it. To make asynchronous requests with HTTPX, you’ll need an AsyncClient. You could control the connection pool size as well, using the limits keyword argument on … Read more

CompletableFuture is not getting executed. If I use the ExecutorService pool it works as expected but not with the common ForkJoinPool

TL;DR: The ForkJoinPool uses daemon threads, whereas the ExecutorService is using non-daemon threads. The latter keep the JVM alive; the former do not. Also, the main thread is a non-daemon thread and when you block it waiting for the CompletableFuture to complete it remains alive (thus keeping the JVM alive). Daemon vs Non-Daemon Threads A … Read more