简体   繁体   中英

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 .

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM