Starmap combined with tqdm?

The simplest way would probably be to apply tqdm() around the inputs, rather than the mapping function. For example: inputs = zip(param1, param2, param3) with mp.Pool(8) as pool: results = pool.starmap(my_function, tqdm.tqdm(inputs, total=len(param1))) Note that the bar is updated when my_function is called, rather than when it returns. If that distinction matters, you can consider … Read more

multiprocessing fork() vs spawn()

There’s a tradeoff between 3 multiprocessing start methods: fork is faster because it does a copy-on-write of the parent process’s entire virtual memory including the initialized Python interpreter, loaded modules, and constructed objects in memory. But fork does not copy the parent process’s threads. Thus locks (in memory) that in the parent process were held … Read more

What’s the point of multithreading in Python if the GIL exists?

In some cases an application may not utilize even one core fully and using threads (or processes) may help to do that. Think of a typical web application. It receives requests from clients, does some queries to the database and returns data back to the client. Given that IO operation is order of magnitude slower … Read more

Python multiprocessing.Queue vs multiprocessing.manager().Queue()

Though my understanding is limited about this subject, from what I did I can tell there is one main difference between multiprocessing.Queue() and multiprocessing.Manager().Queue(): multiprocessing.Queue() is an object whereas multiprocessing.Manager().Queue() is an address (proxy) pointing to shared queue managed by the multiprocessing.Manager() object. therefore you can’t pass normal multiprocessing.Queue() objects to Pool methods, because it … Read more

multiprocessing: map vs map_async

There are four choices to mapping jobs to processes. You have to consider multi-args, concurrency, blocking, and ordering. map and map_async only differ with respect to blocking. map_async is non-blocking where as map is blocking So let’s say you had a function from multiprocessing import Pool import time def f(x): print x*x if __name__ == … Read more

Python multiprocessing.Pool: AttributeError

Error 1: AttributeError: Can’t pickle local object ‘SomeClass.some_method..single’ You solved this error yourself by moving the nested target-function single() out to the top-level. Background: Pool needs to pickle (serialize) everything it sends to its worker-processes (IPC). Pickling actually only saves the name of a function and unpickling requires re-importing the function by name. For that … Read more

Is it possible to run function in a subprocess without threading or writing a separate file/script.

I think you’re looking for something more like the multiprocessing module: http://docs.python.org/library/multiprocessing.html#the-process-class The subprocess module is for spawning processes and doing things with their input/output – not for running functions. Here is a multiprocessing version of your code: from multiprocessing import Process, Queue # must be a global function def my_function(q, x): q.put(x + 100) … Read more

What’s the difference between ThreadPool vs Pool in the multiprocessing module?

The multiprocessing.pool.ThreadPool behaves the same as the multiprocessing.Pool with the only difference that uses threads instead of processes to run the workers logic. The reason you see hi outside of main() being printed multiple times with the multiprocessing.Pool is due to the fact that the pool will spawn 5 independent processes. Each process will initialize … Read more

Sharing a result queue among several processes

Try using multiprocessing.Manager to manage your queue and to also make it accessible to different workers. import multiprocessing def worker(name, que): que.put(“%d is done” % name) if __name__ == ‘__main__’: pool = multiprocessing.Pool(processes=3) m = multiprocessing.Manager() q = m.Queue() workers = pool.apply_async(worker, (33, q))