|
| 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 shared arrays. If |
| 19 | +two threads simultaneously read from and write to the same array, at best they |
| 20 | +will see inconsistent views of the same array data. It is also possible to crash |
| 21 | +the Python interpreter by, for example, resizing an array while another thread |
| 22 | +is reading from it to compute a ufunc operation. |
| 23 | + |
| 24 | +In the future, we may add locking to ndarray to make working with shared NumPy |
| 25 | +arrays easier, but for now we suggest focusing on read-only access of arrays |
| 26 | +that are shared between threads. |
| 27 | + |
| 28 | +Note that operations that *do not* release the GIL will see no performance gains |
| 29 | +from use of the `threading` module, and instead might be better served with |
| 30 | +`multiprocessing`. In particular, operations on arrays with ``dtype=object`` do |
| 31 | +not release the GIL. |
| 32 | + |
| 33 | +Free-threaded Python |
| 34 | +-------------------- |
| 35 | + |
| 36 | +.. versionadded:: 2.1 |
| 37 | + |
| 38 | +Starting with NumPy 2.1 and CPython 3.13, NumPy also has experimental support |
| 39 | +for python runtimes with the GIL disabled. See |
| 40 | +https://py-free-threading.github.io for more information about installing and |
| 41 | +using free-threaded Python, as well as information about supporting it in |
| 42 | +libraries that depend on NumPy. |
| 43 | + |
| 44 | +Because free-threaded Python does not have a global interpreter lock to |
| 45 | +serialize access to Python objects, there are more opportunities for threads to |
| 46 | +mutate shared state and create thread safety issues. In addition to the |
| 47 | +limitations about locking of the ndarray object noted above, this also means |
| 48 | +that arrays with ``dtype=object`` are not protected by the GIL, creating data |
| 49 | +races for python objects that are not possible outside free-threaded python. |
0 commit comments