-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Barrier: Use own implementation every time
That is simpler. Only one implementation can be flawed if bugs occur.
- Loading branch information
Showing
1 changed file
with
17 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
import multiprocessing | ||
import sys | ||
|
||
|
||
# we measure only python 3.4 coverage | ||
if sys.version_info < (3, 3): # pragma: no cover | ||
class Barrier: | ||
def __init__(self, parties): | ||
self.parties = parties | ||
self.__count = multiprocessing.Value('i', 0) | ||
self.__barrier = multiprocessing.Semaphore(0) | ||
class Barrier: | ||
""" | ||
A simple barrier object imitating the multiprocessing.Barrier objects API. | ||
However we cannot use that as it is not available for python 3.2. | ||
""" | ||
def __init__(self, parties): | ||
self.parties = parties | ||
self.__count = multiprocessing.Value('i', 0) | ||
self.__barrier = multiprocessing.Semaphore(0) | ||
|
||
def wait(self): | ||
with self.__count.get_lock(): | ||
self.__count.value += 1 | ||
def wait(self): | ||
with self.__count.get_lock(): | ||
self.__count.value += 1 | ||
|
||
# The last process releases the previous ones and so forth | ||
if self.__count.value == self.parties: | ||
self.__barrier.release() | ||
# The last process releases the previous ones and so forth | ||
if self.__count.value == self.parties: | ||
self.__barrier.release() | ||
|
||
self.__barrier.acquire() | ||
self.__barrier.release() | ||
else: | ||
from multiprocessing import Barrier | ||
self.__barrier.acquire() | ||
self.__barrier.release() |