Skip to content

Commit 9b5de74

Browse files
committed
Delay initialization of multiprocessing.Lock until it's needed
1 parent 88464f6 commit 9b5de74

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

docs/release.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ Fix
1515
~~~
1616
* Fix in-place mutation of input array in `BitRound`.
1717
By :user:`Sam Levang <slevang>`, :issue:`608`
18-
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`
1922

2023
.. _release_0.13.1:
2124

numcodecs/blosc.pyx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,20 @@ 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+
return _MUTEX
8492

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

289297
# set compressor
290298
compressor_set = blosc_set_compressor(cname)
@@ -480,7 +488,7 @@ def _get_use_threads():
480488
proc = multiprocessing.current_process()
481489

482490
# check if locks are available, and if not no threads
483-
if not mutex:
491+
if not get_mutex():
484492
return False
485493

486494
# check for fork

0 commit comments

Comments
 (0)