Skip to content

Commit 9d43084

Browse files
committed
Support nesting and add test
1 parent 4116592 commit 9d43084

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Lib/multiprocessing/synchronize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __getstate__(self):
103103
if sys.platform == 'win32':
104104
h = context.get_spawning_popen().duplicate_for_child(sl.handle)
105105
else:
106-
if self.is_fork_ctx:
106+
if getattr(self, "is_fork_ctx", False):
107107
raise RuntimeError('A SemLock created in a fork context is being '
108108
'shared with a process in a spawn context. This is '
109109
'not supported. Please use the same context to create '

Lib/test/_test_multiprocessing.py

+23
Original file line numberDiff line numberDiff line change
@@ -5342,6 +5342,16 @@ def test_ignore_listener(self):
53425342
finally:
53435343
conn.close()
53445344

5345+
# Utility functions used as target for spawn Process
5346+
def put_one_in_queue(queue):
5347+
queue.put(1)
5348+
5349+
def put_two_and_nest_once(queue):
5350+
queue.put(2)
5351+
process = multiprocessing.Process(target=put_one_in_queue, args=(queue,))
5352+
process.start()
5353+
process.join()
5354+
53455355
class TestStartMethod(unittest.TestCase):
53465356
@classmethod
53475357
def _check_context(cls, conn):
@@ -5443,6 +5453,19 @@ def test_mixed_startmethod(self):
54435453
p.start()
54445454
p.join()
54455455

5456+
def test_nested_startmethod(self):
5457+
queue = multiprocessing.Queue()
5458+
5459+
process = multiprocessing.Process(target=put_two_and_nest_once, args=(queue,))
5460+
process.start()
5461+
process.join()
5462+
5463+
results = []
5464+
while not queue.empty():
5465+
results.append(queue.get())
5466+
5467+
self.assertEqual(results, [2, 1])
5468+
54465469

54475470
@unittest.skipIf(sys.platform == "win32",
54485471
"test semantics don't make sense on Windows")

0 commit comments

Comments
 (0)