| 1 |
from _multiprocessing import SemLock |
| 2 |
from threading import Thread |
| 3 |
import _thread |
| 4 |
import time |
| 5 |
import sys |
| 6 |
import pytest |
| 7 |
|
| 8 |
@pytest.mark.skipif(sys.platform=='win32', reason='segfaults on win32') |
| 9 |
def test_notify_all(): |
| 10 |
"""A low-level variation on test_notify_all() in lib-python's |
| 11 |
_test_multiprocessing.py |
| 12 |
""" |
| 13 |
N_THREADS = 1000 |
| 14 |
lock = SemLock(0, 1, 1, "/test_notify_all", True) |
| 15 |
results = [] |
| 16 |
|
| 17 |
def f(n): |
| 18 |
if lock.acquire(timeout=5.): |
| 19 |
results.append(n) |
| 20 |
lock.release() |
| 21 |
else: |
| 22 |
print("lock acquire timed out!") |
| 23 |
|
| 24 |
threads = [Thread(target=f, args=(i,)) for i in range(N_THREADS)] |
| 25 |
n_started = N_THREADS |
| 26 |
with lock: |
| 27 |
for t in threads: |
| 28 |
try: |
| 29 |
t.start() |
| 30 |
except _thread.error: |
| 31 |
|
| 32 |
t.started = False |
| 33 |
n_started -= 1 |
| 34 |
else: |
| 35 |
t.started = True |
| 36 |
time.sleep(0.1) |
| 37 |
print("started %d threads" % n_started) |
| 38 |
for t in threads: |
| 39 |
if t.started: |
| 40 |
t.join() |
| 41 |
assert len(results) == n_started |