Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7095e9e
Initial commit
YvesDup Mar 5, 2025
89676c1
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 6, 2025
58df0a4
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup Mar 6, 2025
954048f
Fix nits
YvesDup Mar 7, 2025
52144ae
Update test_bounded_semaphore in order to test upper limit on MacOSX
YvesDup Mar 7, 2025
8eb9f30
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup Apr 15, 2025
a8c3925
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup May 25, 2025
be6972f
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup Dec 11, 2025
065921b
refactor code, remove global lock and shared memory when count of sem…
YvesDup Jan 20, 2026
8c8d025
remove unsed 'semlock_traverse' function
YvesDup Jan 20, 2026
56cba95
Fix nits in news file
YvesDup Jan 21, 2026
2cdda38
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup Jan 21, 2026
20cde3b
Fix another nits
YvesDup Jan 21, 2026
14399db
Move variable declaration and refactor code on dump tools
YvesDup Jan 21, 2026
c8a7fbb
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup Jan 21, 2026
006b77c
Merge branch 'python:main' into sem-macosx-multiprocessing-module-C
YvesDup Jan 30, 2026
36e298c
Merge branch 'python:main' into sem-macosx-multiprocessing-module-C
YvesDup Feb 1, 2026
79df976
Merge branch 'python:main' into sem-macosx-multiprocessing-module-C
YvesDup Feb 28, 2026
1a12f54
Merge branch 'python:main' into sem-macosx-multiprocessing-module-C
YvesDup Mar 9, 2026
10e3d6c
Merge branch 'python:main' into sem-macosx-multiprocessing-module-C
YvesDup Mar 12, 2026
b446eff
Merge branch 'python:main' into sem-macosx-multiprocessing-module-C
YvesDup Apr 27, 2026
9c8b9cb
Final version including:
YvesDup May 7, 2026
45017a9
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup May 7, 2026
8af3be7
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup May 7, 2026
3983281
Merge branch 'main' into sem-macosx-multiprocessing-module-C
YvesDup May 7, 2026
ce5dc4c
Fix nits in news
YvesDup May 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/multiprocessing/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ def get_preparation_data(name):
main_path = os.path.join(process.ORIGINAL_DIR, main_path)
d['init_main_from_path'] = os.path.normpath(main_path)

# see gh-125828: workaround on MacOS to emulate `get_value` on Semaphore.
if sys.platform == 'darwin':
import _multiprocessing
d['_macosx_sharedmem_name'] = _multiprocessing._MACOSX_SHAREDMEM_NAME
d['_macosx_shmlock_name'] = _multiprocessing._MACOSX_SHMLOCK_NAME

return d

#
Expand Down Expand Up @@ -245,6 +251,18 @@ def prepare(data):
elif 'init_main_from_path' in data:
_fixup_main_from_path(data['init_main_from_path'])

# see gh-125828: workaround on MacOS to emulate `get_value` on Semaphore.
if sys.platform == 'darwin' and '_macosx_sharedmem_name' in data:
import _multiprocessing
_multiprocessing._set_shm_names(
data['_macosx_sharedmem_name'],
data['_macosx_shmlock_name']
)
# Update module attributes so grandchild processes also get
# the correct names when get_preparation_data() is called.
_multiprocessing._MACOSX_SHAREDMEM_NAME = data['_macosx_sharedmem_name']
_multiprocessing._MACOSX_SHMLOCK_NAME = data['_macosx_shmlock_name']

# Multiprocessing module helpers to fix up the main module in
# spawned subprocesses
def _fixup_main_from_name(mod_name):
Expand Down
71 changes: 65 additions & 6 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ def _resource_unlink(name, rtype):

WAIT_ACTIVE_CHILDREN_TIMEOUT = 5.0

HAVE_GETVALUE = not getattr(_multiprocessing,
'HAVE_BROKEN_SEM_GETVALUE', False)
# Since gh-125828, we no longer need HAVE_GETVALUE.
# This value should be remove from Modules/_multiprocessing/multiprocessing.c.
# when cleanup is complete.
# -------------------
# HAVE_GETVALUE = not getattr(_multiprocessing,
# 'HAVE_BROKEN_SEM_GETVALUE', False)

WIN32 = (sys.platform == "win32")

Expand Down Expand Up @@ -1750,10 +1754,8 @@ def test_semaphore(self):
def test_bounded_semaphore(self):
sem = self.BoundedSemaphore(2)
self._test_semaphore(sem)
# Currently fails on OS/X
#if HAVE_GETVALUE:
# self.assertRaises(ValueError, sem.release)
# self.assertReturnsIfImplemented(2, get_value, sem)
self.assertRaises(ValueError, sem.release)
self.assertReturnsIfImplemented(2, get_value, sem)

def test_timeout(self):
if self.TYPE != 'processes':
Expand Down Expand Up @@ -7380,6 +7382,63 @@ def test_preload_main_large_sys_argv(self):
'',
])

#
# Tests for workaround macOSX Semaphore
#

ACQUIRE, RELEASE = range(2)
@unittest.skipIf(sys.platform != "darwin", "MacOSX only")
class _TestMacOSXSemaphore(BaseTestCase):
ALLOWED_TYPES = ('processes',)
@classmethod
def _run_thread(cls, sem, meth, ntime, delay):
if meth == ACQUIRE:
for _ in range(ntime):
sem.acquire()
time.sleep(delay)
else:
for _ in range(ntime):
sem.release()
time.sleep(delay)

@classmethod
def _run_process(cls, sem, sem_meth, nthread=1, ntime=10, delay=0.1):
ts = []
for _ in range(nthread):
t = threading.Thread(target=cls._run_thread,
args=(sem, sem_meth, ntime, delay))
ts.append(t)
for t in ts:
t.start()
for t in ts:
t.join()

def test_mix_several_acquire_release(self):
# n processes, threads per process and loops per threads
n_p_acq, n_th_acq, n_loop_acq = 15, 5, 20
n_p_rel, n_th_rel, n_loop_rel = 8, 8, 8

n_acq = n_p_acq*n_th_acq*n_loop_acq
n_rel = n_p_rel*n_th_rel*n_loop_rel
sem = self.Semaphore(n_acq)
ps = []
for _ in range(n_p_acq):
p = self.Process(target=self._run_process,
args=(sem, ACQUIRE, n_th_acq, n_loop_acq, 0.01))
ps.append(p)

for _ in range(n_p_rel):
p = self.Process(target=self._run_process,
args=(sem, RELEASE, n_th_rel, n_loop_rel, 0.005))
ps.append(p)

for p in ps:
p.start()
for p in ps:
p.join()
self.assertEqual(sem.get_value(), n_rel)


#
# Mixins
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the not implemented ``get_value`` for :class:`multiprocessing.Semaphore` on MacOSX by adding a dedicated workaround in ``_multiprocessing.SemLock`` object.
54 changes: 33 additions & 21 deletions Modules/_multiprocessing/clinic/semaphore.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Modules/_multiprocessing/multiprocessing.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ static PyMethodDef module_methods[] = {
#endif
#if !defined(POSIX_SEMAPHORES_NOT_ENABLED)
_MULTIPROCESSING_SEM_UNLINK_METHODDEF
#endif
#ifdef HAVE_BROKEN_SEM_GETVALUE
{"_set_shm_names", _multiprocessing_set_shm_names, METH_VARARGS,
"Set shared memory and glock names (used by spawned child processes)."},
#endif
{NULL}
};
Expand Down Expand Up @@ -230,6 +234,11 @@ multiprocessing_exec(PyObject *module)
}
Py_DECREF(py_sem_value_max);

#ifdef HAVE_BROKEN_SEM_GETVALUE
if (_PyMp_init_shm_names(module) < 0)
return -1;
#endif

#endif

/* Add configuration macros */
Expand Down
4 changes: 4 additions & 0 deletions Modules/_multiprocessing/multiprocessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,9 @@ PyObject *_PyMp_SetError(PyObject *Type, int num);

extern PyType_Spec _PyMp_SemLockType_spec;
extern PyObject *_PyMp_sem_unlink(const char *name);
#ifdef HAVE_BROKEN_SEM_GETVALUE
extern int _PyMp_init_shm_names(PyObject *module);
extern PyObject *_multiprocessing_set_shm_names(PyObject *module, PyObject *args);
#endif

#endif /* MULTIPROCESSING_H */
Loading
Loading