Skip to content

Commit

Permalink
Support nesting and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
albanD committed Aug 29, 2023
1 parent d08d49d commit ec4e7e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/multiprocessing/synchronize.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __getstate__(self):
if sys.platform == 'win32':
h = context.get_spawning_popen().duplicate_for_child(sl.handle)
else:
if self.is_fork_ctx:
if getattr(self, "is_fork_ctx", False):
raise RuntimeError('A SemLock created in a fork context is being '
'shared with a process in a spawn context. This is '
'not supported. Please use the same context to create '
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5342,6 +5342,16 @@ def test_ignore_listener(self):
finally:
conn.close()

# Utility functions used as target for spawn Process
def put_one_in_queue(queue):
queue.put(1)

def put_two_and_nest_once(queue):
queue.put(2)
process = multiprocessing.Process(target=put_one_in_queue, args=(queue,))
process.start()
process.join()

class TestStartMethod(unittest.TestCase):
@classmethod
def _check_context(cls, conn):
Expand Down Expand Up @@ -5443,6 +5453,19 @@ def test_mixed_startmethod(self):
p.start()
p.join()

def test_nested_startmethod(self):
queue = multiprocessing.Queue()

process = multiprocessing.Process(target=put_two_and_nest_once, args=(queue,))
process.start()
process.join()

results = []
while not queue.empty():
results.append(queue.get())

self.assertEqual(results, [2, 1])


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

0 comments on commit ec4e7e0

Please sign in to comment.