Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Fix
~~~
* Fix in-place mutation of input array in `BitRound`.
By :user:`Sam Levang <slevang>`, :issue:`608`
* Fix an issue where importing numcodecs would lock the state of `multiprocessing`
and prevent user code to call `multiprocessing.set_start_method("spawn")`
subsequently.
By :user:`Clément Robert <neutrinoceros>` :issue:`522`

Enhancements
~~~~~~~~~~~~
Expand Down
25 changes: 17 additions & 8 deletions numcodecs/blosc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,21 @@ AUTOSHUFFLE = -1
AUTOBLOCKS = 0

# synchronization
try:
mutex = multiprocessing.Lock()
except OSError:
mutex = None
except ImportError:
mutex = None
_MUTEX = None
_MUTEX_IS_INIT = False

def get_mutex():
global _MUTEX_IS_INIT, _MUTEX
if not _MUTEX_IS_INIT:
try:
mutex = multiprocessing.Lock()
except OSError:
mutex = None
except ImportError:
mutex = None
_MUTEX = mutex
_MUTEX_IS_INIT = True
return _MUTEX

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

# set compressor
compressor_set = blosc_set_compressor(cname)
Expand Down Expand Up @@ -480,7 +489,7 @@ def _get_use_threads():
proc = multiprocessing.current_process()

# check if locks are available, and if not no threads
if not mutex:
if not get_mutex():
return False

# check for fork
Expand Down
Loading