|
| 1 | +.. _thread_safety: |
| 2 | + |
| 3 | +************* |
| 4 | +Thread Safety |
| 5 | +************* |
| 6 | + |
| 7 | +NumPy supports use in a multithreaded context via the `threading` module in the |
| 8 | +standard library. Many NumPy operations release the GIL, so unlike many |
| 9 | +situations in Python, it is possible to improve parallel performance by |
| 10 | +exploiting multithreaded parallelism in Python. |
| 11 | + |
| 12 | +The easiest performance gains happen when each worker thread owns its own array |
| 13 | +or set of array objects, with no data directly shared between threads. Because |
| 14 | +NumPy releases the GIL for many low-level operations, threads that spend most of |
| 15 | +the time in low-level code will run in parallel. |
| 16 | + |
| 17 | +It is possible to share NumPy arrays between threads, but extreme care must be |
| 18 | +taken to avoid creating thread safety issues when mutating arrays that are |
| 19 | +shared between multiple threads. If two threads simultaneously read from and |
| 20 | +write to the same array, they will at best produce inconsistent, racey results that |
| 21 | +are not reproducible, let alone correct. It is also possible to crash the Python |
| 22 | +interpreter by, for example, resizing an array while another thread is reading |
| 23 | +from it to compute a ufunc operation. |
| 24 | + |
| 25 | +In the future, we may add locking to ndarray to make writing multithreaded |
| 26 | +algorithms using NumPy arrays safer, but for now we suggest focusing on |
| 27 | +read-only access of arrays that are shared between threads, or adding your own |
| 28 | +locking if you need to mutation and multithreading. |
| 29 | + |
| 30 | +Note that operations that *do not* release the GIL will see no performance gains |
| 31 | +from use of the `threading` module, and instead might be better served with |
| 32 | +`multiprocessing`. In particular, operations on arrays with ``dtype=object`` do |
| 33 | +not release the GIL. |
| 34 | + |
| 35 | +Free-threaded Python |
| 36 | +-------------------- |
| 37 | + |
| 38 | +.. versionadded:: 2.1 |
| 39 | + |
| 40 | +Starting with NumPy 2.1 and CPython 3.13, NumPy also has experimental support |
| 41 | +for python runtimes with the GIL disabled. See |
| 42 | +https://py-free-threading.github.io for more information about installing and |
| 43 | +using free-threaded Python, as well as information about supporting it in |
| 44 | +libraries that depend on NumPy. |
| 45 | + |
| 46 | +Because free-threaded Python does not have a global interpreter lock to |
| 47 | +serialize access to Python objects, there are more opportunities for threads to |
| 48 | +mutate shared state and create thread safety issues. In addition to the |
| 49 | +limitations about locking of the ndarray object noted above, this also means |
| 50 | +that arrays with ``dtype=object`` are not protected by the GIL, creating data |
| 51 | +races for python objects that are not possible outside free-threaded python. |
0 commit comments