Thread Safety#
NumPy supports use in a multithreaded context via thethreading module in thestandard library. Many NumPy operations release the GIL, so unlike manysituations in Python, it is possible to improve parallel performance byexploiting multithreaded parallelism in Python.
The easiest performance gains happen when each worker thread owns its own arrayor set of array objects, with no data directly shared between threads. BecauseNumPy releases the GIL for many low-level operations, threads that spend most ofthe time in low-level code will run in parallel.
It is possible to share NumPy arrays between threads, but extreme care must betaken to avoid creating thread safety issues when mutating arrays that areshared between multiple threads. If two threads simultaneously read from andwrite to the same array, they will at best produce inconsistent, racey results thatare not reproducible, let alone correct. It is also possible to crash the Pythoninterpreter by, for example, resizing an array while another thread is readingfrom it to compute a ufunc operation.
In the future, we may add locking to ndarray to make writing multithreadedalgorithms using NumPy arrays safer, but for now we suggest focusing onread-only access of arrays that are shared between threads, or adding your ownlocking if you need to mutation and multithreading.
Note that operations thatdo not release the GIL will see no performance gainsfrom use of thethreading module, and instead might be better served withmultiprocessing. In particular, operations on arrays withdtype=object donot release the GIL.
Free-threaded Python#
New in version 2.1.
Starting with NumPy 2.1 and CPython 3.13, NumPy also has experimental supportfor python runtimes with the GIL disabled. Seehttps://py-free-threading.github.io for more information about installing andusing free-threaded Python, as well as information about supporting it inlibraries that depend on NumPy.
Because free-threaded Python does not have a global interpreter lock toserialize access to Python objects, there are more opportunities for threads tomutate shared state and create thread safety issues. In addition to thelimitations about locking of the ndarray object noted above, this also meansthat arrays withdtype=object are not protected by the GIL, creating dataraces for python objects that are not possible outside free-threaded python.