Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Python multiprocessing Pool thread safe?

Tags:

python

django

I have Django project. If I make package variable that contains Pool() object, and will try to use that Pool from Django views (which run in parallel way), will be this way thread safe? Are there any others ways to do it?

from multiprocessing import Pool
general_executor_pool = Pool()
like image 894
Sergey Luchko Avatar asked Oct 31 '16 22:10

Sergey Luchko


People also ask

Is Python multiprocessing queue thread safe?

Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes: Queues are thread and process safe.

Is Python ThreadPoolExecutor thread safe?

ThreadPoolExecutor Thread-Safety Although the ThreadPoolExecutor uses threads internally, you do not need to work with threads directly in order to execute tasks and get results. Nevertheless, when accessing resources or critical sections, thread-safety may be a concern.

Is ThreadPoolExecutor thread safe?

For ThreadPoolExecutor the answer is simply yes. ExecutorService does not mandate or otherwise guarantee that all implementations are thread-safe, and it cannot as it is an interface. These types of contracts are outside of the scope of a Java interface.

Is Python good for multiprocessing?

If your code is IO bound, both multiprocessing and multithreading in Python will work for you. Multiprocessing is a easier to just drop in than threading but has a higher memory overhead.


Video Answer


1 Answers

I found this question via Google as I'm asking the same question. Anecdotally I can say NO it is not, because I recently debugged a piece of software that suffered from race conditions. Here's how it went:

  1. A master process ran in a loop and spawned a multiprocessing pool in a new thread eery 3 minutes with a list of ~1000 accounts to be acted on
  2. The thread called multiprocessing.Pool(max_proccesses=32), pool.map(func, accounts). This would open 32 processes, and one by one apply each account to an available process.
  3. Unbeknownst to the original author, this process took far longer to complete than 3 minutes. So what happened the next time a thread was spawned to create a multiprocessing pool? Did it spawn 32 new processes for a total of 64? No, in practice it did not. Instead my results were scrambled and showed indication that multiple threads were acting on my data in a non-deterministic way.

I'd love to trace through the multiprocessing module to see if it is un-thread-safe by design, or get an answer from someone in the know. Anecdotally, at least, I have witnessed first hand that it is not.

like image 80
user3681414 Avatar answered Oct 02 '22 15:10

user3681414