{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {},
  "_ordered_sections": [],
  "item_file": null,
  "item_line": null,
  "item_type": null,
  "aliases": [],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": null,
  "references": null,
  "qa": "reference:thread_safety",
  "arbitrary": [
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "NumPy supports use in a multithreaded context via the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "threading",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "threading",
                "version": "*",
                "kind": "api",
                "path": "threading"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " module in the standard library. Many NumPy operations release the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "python:GIL"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", so unlike many situations in Python, it is possible to improve parallel performance by exploiting multithreaded parallelism in Python."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The easiest performance gains happen when each worker thread owns its own array or set of array objects, with no data directly shared between threads. Because NumPy releases the GIL for many low-level operations, threads that spend most of the time in low-level code will run in parallel."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "It is possible to share NumPy arrays between threads, but extreme care must be 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."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "In the future, we may add locking to "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "ndarray",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "numpy",
                "version": "*",
                "kind": "api",
                "path": "numpy:ndarray"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " 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."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Note that operations that "
            },
            {
              "__type": "Emphasis",
              "__tag": 4047,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "do not"
                }
              ]
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " release the GIL will see no performance gains from use of the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "threading",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "threading",
                "version": "*",
                "kind": "api",
                "path": "threading"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " module, and instead might be better served with "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "multiprocessing",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "multiprocessing",
                "version": "*",
                "kind": "api",
                "path": "multiprocessing"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". In particular, operations on arrays with "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "dtype=np.object_"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " do not release the GIL."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Thread Safety"
        }
      ],
      "level": 0,
      "target": "thread_safety"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "NumPy maintains some state for ufuncs context-local basis, which means each thread in a multithreaded program or task in an asyncio program has its own independent configuration of the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "numpy.errstate",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "numpy",
                "version": "*",
                "kind": "api",
                "path": "numpy:errstate"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " (see "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "/reference/routines.err",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "/reference/routines.err"
              },
              "kind": "docs"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "), and of "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "text_formatting_options",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "reference:routines.io"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "You can update state stored in a context variable by entering a context manager. As soon as the context manager exits, the state will be reset to its value before entering the context manager."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Context-local state"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Admonition",
          "__tag": 4056,
          "kind": "versionadded",
          "base_type": "neutral",
          "children": [
            {
              "__type": "AdmonitionTitle",
              "__tag": 4055,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "versionadded 2.1"
                }
              ]
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Starting with NumPy 2.1 and CPython 3.13, NumPy also has experimental support for python runtimes with the GIL disabled. See https://py-free-threading.github.io for more information about installing and using "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "free-threaded <python:free threading>"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " Python, as well as information about supporting it in libraries that depend on NumPy."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Because free-threaded Python does not have a global interpreter lock to serialize access to Python objects, there are more opportunities for threads to mutate shared state and create thread safety issues. In addition to the limitations about locking of the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "ndarray",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "numpy",
                "version": "*",
                "kind": "api",
                "path": "numpy:ndarray"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " object noted above, this also means that arrays with "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "dtype=np.object_"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are not protected by the GIL, creating data races for python objects that are not possible outside free-threaded python."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Free-threaded Python"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "For developers writing C extensions that interact with NumPy, several parts of the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "C-API array documentation",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "/reference/c-api/array"
              },
              "kind": "docs"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " provide detailed information about multithreading considerations."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "C-API Threading Support"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "BulletList",
          "__tag": 4053,
          "ordered": false,
          "start": 1,
          "children": [
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "/reference/random/multithreading",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "docs",
                        "path": "/reference/random/multithreading"
                      },
                      "kind": "docs"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " - Practical example of using NumPy's   random number generators in a multithreaded context with   "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "concurrent.futures",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": "concurrent",
                        "version": "*",
                        "kind": "api",
                        "path": "concurrent.futures"
                      },
                      "kind": "module"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "See Also"
        }
      ],
      "level": 1,
      "target": null
    }
  ],
  "local_refs": []
}