Search code examples
pythonmultithreadingpathos

How can I make sure that two function calls are always made consecutively with multiple threads in Python?


I'm using Pathos like this:

from pathos.multiprocessing import ProcessingPool as Pool
def foo(bar):
   fn1(bar)
   fn2(bar)

Pool().map(foo, data)

I want fn1 and fn2 to be executed as one atomic operation such that no threads can produce function calls in a sequence like fn1, fn1, fn2, fn2.


Solution

  • You need to use a lock like it is described in the documentation.

    def foo(lock, bar):
        lock.acquire()
        try:
           fn1(bar)
           fn2(bar)
        finally:
            lock.release()
    
    if __name__ == '__main__':
        lock = Lock()
        Pool().map(foo, [(l, d) for d in data])
    

    But, why are you using multiprocessing if you don't want to call your function in parallel?