def test_semaphore_basic(self): from _multiprocessing import SemLock import sys assert SemLock.SEM_VALUE_MAX > 10 kind = self.SEMAPHORE value = 1 maxvalue = 1 # the following line gets OSError: [Errno 38] Function not implemented # if /dev/shm is not mounted on Linux sem = SemLock(kind, value, maxvalue) assert sem.kind == kind assert sem.maxvalue == maxvalue assert isinstance(sem.handle, (int, long)) assert sem._count() == 0 if sys.platform == 'darwin': raises(NotImplementedError, 'sem._get_value()') else: assert sem._get_value() == 1 assert sem._is_zero() == False sem.acquire() assert sem._is_mine() assert sem._count() == 1 if sys.platform == 'darwin': raises(NotImplementedError, 'sem._get_value()') else: assert sem._get_value() == 0 assert sem._is_zero() == True sem.release() assert sem._count() == 0 sem.acquire() sem._after_fork() assert sem._count() == 0
def test_semaphore(self): from _multiprocessing import SemLock import sys assert SemLock.SEM_VALUE_MAX > 10 kind = self.SEMAPHORE value = 1 maxvalue = 1 sem = SemLock(kind, value, maxvalue) assert sem.kind == kind assert sem.maxvalue == maxvalue assert isinstance(sem.handle, (int, long)) assert sem._count() == 0 if sys.platform == 'darwin': raises(NotImplementedError, 'sem._get_value()') else: assert sem._get_value() == 1 assert sem._is_zero() == False sem.acquire() assert sem._is_mine() assert sem._count() == 1 if sys.platform == 'darwin': raises(NotImplementedError, 'sem._get_value()') else: assert sem._get_value() == 0 assert sem._is_zero() == True sem.release() assert sem._count() == 0 sem.acquire() sem._after_fork() assert sem._count() == 0
def test_semaphore(self): from _multiprocessing import SemLock import sys assert SemLock.SEM_VALUE_MAX > 10 kind = self.SEMAPHORE value = 1 maxvalue = 1 # the following line gets OSError: [Errno 38] Function not implemented # if /dev/shm is not mounted on Linux sem = SemLock(kind, value, maxvalue) assert sem.kind == kind assert sem.maxvalue == maxvalue assert isinstance(sem.handle, (int, long)) assert sem._count() == 0 if sys.platform == "darwin": raises(NotImplementedError, "sem._get_value()") else: assert sem._get_value() == 1 assert sem._is_zero() == False sem.acquire() assert sem._is_mine() assert sem._count() == 1 if sys.platform == "darwin": raises(NotImplementedError, "sem._get_value()") else: assert sem._get_value() == 0 assert sem._is_zero() == True sem.release() assert sem._count() == 0 sem.acquire() sem._after_fork() assert sem._count() == 0
def test_semaphore_maxvalue(self): from _multiprocessing import SemLock import sys kind = self.SEMAPHORE value = SemLock.SEM_VALUE_MAX maxvalue = SemLock.SEM_VALUE_MAX sem = SemLock(kind, value, maxvalue) for i in range(10): res = sem.acquire() assert res == True assert sem._count() == i + 1 if sys.platform != 'darwin': assert sem._get_value() == maxvalue - (i + 1) value = 0 maxvalue = SemLock.SEM_VALUE_MAX sem = SemLock(kind, value, maxvalue) for i in range(10): sem.release() assert sem._count() == -(i + 1) if sys.platform != 'darwin': assert sem._get_value() == i + 1
def test_recursive(self): from _multiprocessing import SemLock kind = self.RECURSIVE value = 1 maxvalue = 1 sem = SemLock(kind, value, maxvalue) sem.acquire() sem.release() assert sem._count() == 0 sem.acquire() sem.release() # now recursively sem.acquire() sem.acquire() assert sem._count() == 2 sem.release() sem.release()
def test_recursive(self): from _multiprocessing import SemLock kind = self.RECURSIVE value = 1 maxvalue = 1 # the following line gets OSError: [Errno 38] Function not implemented # if /dev/shm is not mounted on Linux sem = SemLock(kind, value, maxvalue, "2", unlink=True) sem.acquire() sem.release() assert sem._count() == 0 sem.acquire() sem.release() # now recursively sem.acquire() sem.acquire() assert sem._count() == 2 sem.release() sem.release()
def test_recursive(self): from _multiprocessing import SemLock kind = self.RECURSIVE value = 1 maxvalue = 1 # the following line gets OSError: [Errno 38] Function not implemented # if /dev/shm is not mounted on Linux sem = SemLock(kind, value, maxvalue) sem.acquire() sem.release() assert sem._count() == 0 sem.acquire() sem.release() # now recursively sem.acquire() sem.acquire() assert sem._count() == 2 sem.release() sem.release()
class SemLock(object): _rand = tempfile._RandomNameSequence() def __init__(self, kind, value, maxvalue): name = 'loky' unlink_now = sys.platform == 'win32' or name == 'fork' for i in range(100): try: self._semlock = SemLockC(kind, value, maxvalue, SemLock._make_name(), unlink_now) except FileExistsError: pass else: break else: raise FileExistsError('cannot find name for semaphore') self._make_methods() if sys.platform != 'win32': def _after_fork(obj): obj._semlock._after_fork() util.register_after_fork(self, _after_fork) if self._semlock.name is not None: # We only get here if we are on Unix with forking # disabled. When the object is garbage collected or the # process shuts down we unlink the semaphore name from .semaphore_tracker import register if sys.version_info < (3, 4): register(self._semlock.name) util.Finalize(self, SemLock._cleanup, (self._semlock.name, ), exitpriority=0) @staticmethod def _cleanup(name): from .semaphore_tracker import unregister sem_unlink(name) unregister(name) def _make_methods(self): self.acquire = self._semlock.acquire self.release = self._semlock.release def __enter__(self): return self._semlock.acquire() def __exit__(self, *args): return self._semlock.release(*args) def __getstate__(self): assert is_spawning() sl = self._semlock if sys.platform == 'win32': h = get_spawning_popen().duplicate_for_child(sl.handle) else: h = sl.handle return (h, sl.kind, sl.maxvalue, sl.name) def __setstate__(self, state): if sys.version_info < (3, 4): h, kind, maxvalue, name = state self._semlock = SemLockC(h, kind, maxvalue, name=name) else: self._semlock = SemLockC._rebuild(*state) util.debug('recreated blocker with handle %r and name %s' % (state[0], state[3])) self._make_methods() @staticmethod def _make_name(): if sys.version_info < (3, 4): return None return '%s-%s' % (process.current_process()._config['semprefix'], next(SemLock._rand))