0

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.

Lenar Hoyt
  • 5,971
  • 6
  • 49
  • 59

1 Answers1

1

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?

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Please, not the OP is not using Python's multiprocessing, but rather the pathos project: http://trac.mystic.cacr.caltech.edu/project/pathos/wiki.html – jsbueno May 09 '17 at 19:24
  • Hi I'm the `pathos` author. There's a nice middle ground... if you use `multiprocess` instead of `multiprocessing`. This is the library that `pathos` uses under the covers, and it provides the exact same interface as `multiprocessing`. So, if you use `pathos` and combine it with really what's just a different interface in `multiprocess`, it's fully compatible. – Mike McKerns May 10 '17 at 12:06