A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/51351609/can-i-const-access-cpython-objects-without-gil below:

performance - Can I const-access CPython objects without GIL?

Context and motivation

This is a question about releasing GIL in CPython while still working with Python objects, but in a limited way.

For performance reasons, I need to jump into GIL-less mode while maintaining read-only access to existing Python objects. The objects are guaranteed to exist throughout, and no other thread mutates them (no deletes, appends etc). In other words, they are "const" for all practical purposes, no ref counting needed, no dynamic allocations take place.

Question

Assume we have a native Python object on input, such as a list, dict or int. We can work with this object as PyObject * using the C API, as normal.

Q: Is it safe to release GIL and then call read-only ("const") functions on this object? For example, call PyList_GET_ITEM(lst, 0), PyInt_AS_LONG(i) or PyDict_GetItem(dct, key)?

If not, why not?

More generally, what is the CPython contract regarding (lack of) object mutation and dynamic allocations, and the need for synchronization via GIL?

What I tried

I created a Python list of integers, then released the GIL, iterated over the list elements using standard PyList_Size and PyList_GET_ITEM, summed the integer values inside, re-acquired GIL. All worked fine, results came out correct. No need to incref/decref anything, since no Python objects were created, deleted or mutated.

But this experiment is no proof of course—there may be side effects or limitations.

What I'm NOT asking

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4