Using Global Interpreter Locks (GIL) when developing Python code in C++

Python uses a Global Interpreter Lock (GIL) to ensures that Python code in only executing in a single thread at a time.

When you use the Python C API and/or expose C++ code to Python, you have to re-acquire the GIL each time you call in a C Python API. This is also true when writing Python extensions for Maya or when using the Python C API from a Maya plug-ins.

You will need to add these calls in a few high-level places in your code, like this:

PyGILState_STATE state = PyGILState_Ensure();
// call your python code here:
PyGILState_Release(state);

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading