Skip to content

Commit

Permalink
Delay initialization of multiprocessing.Lock until it's needed
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Oct 23, 2024
1 parent 88464f6 commit d7a4363
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 4 additions & 1 deletion docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +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`

.. _release_0.13.1:

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

0 comments on commit d7a4363

Please sign in to comment.