8000 DOC: add docs on thread safety in NumPy by charris · Pull Request #27233 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOC: add docs on thread safety in NumPy #27233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
DOC: reword discussion about shared arrays to hopefully be clearer
[skip azp][skip actions][skip cirrus]
  • Loading branch information
ngoldbaum authored and charris committed Aug 17, 2024
commit 395a81dee5be52b2acff817e6f16652d23181fc3
20 changes: 11 additions & 9 deletions doc/source/reference/thread_safety.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ NumPy releases the GIL for many low-level operations, threads that spend most of
the time in low-level code will run in parallel.

It is possible to share NumPy arrays between threads, but extreme care must be
taken to avoid creating thread safety issues when mutating shared arrays. If
two threads simultaneously read from and write to the same array, at best they
will see inconsistent views of the same array data. It is also possible to crash
the Python interpreter by, for example, resizing an array while another thread
is reading from it to compute a ufunc operation.

In the future, we may add locking to ndarray to make working with shared NumPy
arrays easier, but for now we suggest focusing on read-only access of arrays
that are shared between threads.
taken to avoid creating thread safety issues when mutating arrays that are
shared between multiple threads. If two threads simultaneously read from and
write to the same array, they will at best produce inconsistent, racey results that
are not reproducible, let alone correct. It is also possible to crash the Python
interpreter by, for example, resizing an array while another thread is reading
from it to compute a ufunc operation.

In the future, we may add locking to ndarray to make writing multithreaded
algorithms using NumPy arrays safer, but for now we suggest focusing on
read-only access of arrays that are shared between threads, or adding your own
locking if you need to mutation and multithreading.

Note that operations that *do not* release the GIL will see no performance gains
from use of the `threading` module, and instead might be better served with
Expand Down
Loading
0