summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Burke <tim.burke@gmail.com>2020-11-02 16:09:46 -0800
committerJakub Stasiak <jakub@stasiak.at>2020-11-03 08:48:13 +0100
commit087ba743c7af8a40ac1e4e2ec89409eee3b4233e (patch)
treea483f3eba39b13228ef5265efe4b931f15b1f827
parent9d731373e2325588a33229b425100ad538eed766 (diff)
downloadeventlet-087ba743c7af8a40ac1e4e2ec89409eee3b4233e.tar.gz
py39: Add _at_fork_reinit method to Semaphores
CPython expects to be able to call such a method on RLocks, Conditions, and Events in threading; since we may monkey-patch threading to use Semaphores as locks, they need the method, too. Addresses #646
-rw-r--r--eventlet/semaphore.py5
-rw-r--r--tests/semaphore_test.py21
2 files changed, 26 insertions, 0 deletions
diff --git a/eventlet/semaphore.py b/eventlet/semaphore.py
index 18b5b05..5e2b5e3 100644
--- a/eventlet/semaphore.py
+++ b/eventlet/semaphore.py
@@ -39,6 +39,7 @@ class Semaphore(object):
if value < 0:
msg = 'Semaphore() expect value >= 0, actual: {0}'.format(repr(value))
raise ValueError(msg)
+ self._original_value = value
self.counter = value
self._waiters = collections.deque()
@@ -51,6 +52,10 @@ class Semaphore(object):
params = (self.__class__.__name__, self.counter, len(self._waiters))
return '<%s c=%s _w[%s]>' % params
+ def _at_fork_reinit(self):
+ self.counter = self._original_value
+ self._waiters.clear()
+
def locked(self):
"""Returns true if a call to acquire would block.
"""
diff --git a/tests/semaphore_test.py b/tests/semaphore_test.py
index d6c11d1..cf6a29d 100644
--- a/tests/semaphore_test.py
+++ b/tests/semaphore_test.py
@@ -42,6 +42,27 @@ class TestSemaphore(tests.LimitedTestCase):
sem = eventlet.Semaphore()
self.assertRaises(ValueError, sem.acquire, blocking=False, timeout=1)
+ def test_reinit(self):
+ # py39+ expects locks to have a _at_fork_reinit() method; since we
+ # patch in Semaphores in eventlet.green.thread, they need it, too
+ sem = eventlet.Semaphore()
+ sem.acquire()
+ sem._at_fork_reinit()
+ self.assertEqual(sem.acquire(blocking=False), True)
+ self.assertEqual(sem.acquire(blocking=False), False)
+
+ sem = eventlet.Semaphore(0)
+ sem.release()
+ sem._at_fork_reinit()
+ self.assertEqual(sem.acquire(blocking=False), False)
+
+ sem = eventlet.Semaphore(2)
+ sem.acquire()
+ sem._at_fork_reinit()
+ self.assertEqual(sem.acquire(blocking=False), True)
+ self.assertEqual(sem.acquire(blocking=False), True)
+ self.assertEqual(sem.acquire(blocking=False), False)
+
def test_semaphore_contention():
g_mutex = eventlet.Semaphore()