4

I am using Python 2.7, and have been converting multithreaded code to multiprocessing code to avoid GIL lock problems. However, I don't see a barrier implementation in the multiprocessing module (Any ideas how to implement one?).

I saw this question: Is it possible to use multiprocessing.Event to implement a synchronization barrier for pool of processes? But I'm not sure whether it will work correctly because it doesn't use any locks!

Thanks!

Community
  • 1
  • 1
mas
  • 345
  • 5
  • 18

2 Answers2

3

I am pretty sure that the built-in synchronization primitives of the multiprocessing package provide what you need: https://docs.python.org/2/library/multiprocessing.html#synchronization-primitives

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
  • ok thanks. I found this code in this thread: http://stackoverflow.com/questions/26622745/implementing-barrier-in-python2-7, i hope using it with semaphores from the multiprocessing module should work ok. class Barrier: def __init__(self, n): self.n = n self.count = 0 self.mutex = Semaphore(1) self.barrier = Semaphore(0) def wait(self): self.mutex.acquire() self.count = self.count + 1 self.mutex.release() if self.count == self.n: self.barrier.release() self.barrier.acquire() self.barrier.release() – mas Feb 06 '15 at 22:29
1

Here is the analogy for multiprocessing, as for threading from here http://stackoverflow.com/questions/26622745/implementing-barrier-in-python2-7:

from multiprocessing import Process, Semaphore, Value

class Barrier:
    def __init__(self, n):
        self.n       = n
        self.count   = Value('i', 0)
        self.mutex   = Semaphore(1)
        self.barrier = Semaphore(0)

    def wait(self):
        self.mutex.acquire()
        self.count.value += 1
        self.mutex.release()

        if self.count.value == self.n:
            self.barrier.release()

        self.barrier.acquire()
        self.barrier.release()

Then the example of usage in the main thread:

barrier = Barrier(2)
process = Process(target = my_second_thread, args = (barrier,))
process.start()
JenyaKh
  • 2,040
  • 17
  • 25