Skip to content

Commit 230bc01

Browse files
Delay initialization of multiprocessing.Lock until it's needed (#615)
Co-authored-by: David Stansby <[email protected]>
1 parent e3299b8 commit 230bc01

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

docs/release.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Fix
1515
~~~
1616
* Fix in-place mutation of input array in `BitRound`.
1717
By :user:`Sam Levang <slevang>`, :issue:`608`
18+
* Fix an issue where importing numcodecs would lock the state of `multiprocessing`
19+
and prevent user code to call `multiprocessing.set_start_method("spawn")`
20+
subsequently.
21+
By :user:`Clément Robert <neutrinoceros>` :issue:`522`
1822

1923
Enhancements
2024
~~~~~~~~~~~~

numcodecs/blosc.pyx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,21 @@ AUTOSHUFFLE = -1
7575
AUTOBLOCKS = 0
7676

7777
# synchronization
78-
try:
79-
mutex = multiprocessing.Lock()
80-
except OSError:
81-
mutex = None
82-
except ImportError:
83-
mutex = None
78+
_MUTEX = None
79+
_MUTEX_IS_INIT = False
80+
81+
def get_mutex():
82+
global _MUTEX_IS_INIT, _MUTEX
83+
if not _MUTEX_IS_INIT:
84+
try:
85+
mutex = multiprocessing.Lock()
86+
except OSError:
87+
mutex = None
88+
except ImportError:
89+
mutex = None
90+
_MUTEX = mutex
91+
_MUTEX_IS_INIT = True
92+
return _MUTEX
8493

8594
# store ID of process that first loads the module, so we can detect a fork later
8695
_importer_pid = os.getpid()
@@ -284,7 +293,7 @@ def compress(source, char* cname, int clevel, int shuffle=SHUFFLE,
284293
# N.B., we are using blosc's global context, and so we need to use a lock
285294
# to ensure no-one else can modify the global context while we're setting it
286295
# up and using it.
287-
with mutex:
296+
with get_mutex():
288297

289298
# set compressor
290299
compressor_set = blosc_set_compressor(cname)
@@ -480,7 +489,7 @@ def _get_use_threads():
480489
proc = multiprocessing.current_process()
481490

482491
# check if locks are available, and if not no threads
483-
if not mutex:
492+
if not get_mutex():
484493
return False
485494

486495
# check for fork

0 commit comments

Comments
 (0)