Skip to content

BUG: Release the Python Global Interpreter Lock (GIL) during execution#1065

Merged
thewtex merged 3 commits intoInsightSoftwareConsortium:releasefrom
thewtex:release-gil
Jul 12, 2019
Merged

BUG: Release the Python Global Interpreter Lock (GIL) during execution#1065
thewtex merged 3 commits intoInsightSoftwareConsortium:releasefrom
thewtex:release-gil

Conversation

@thewtex
Copy link
Member

@thewtex thewtex commented Jul 6, 2019

Release the Python Global Interpreter Lock (GIL) so multi-threaded
Python applications, like Dask, can run in parallel while ITK does heavy
operations.

For more information, see section 32.13 Support for Multithreaded
Applications at

http://www.swig.org/Doc4.0/Python.html#Python

Also, set nothreadallow as suggested in the comments
on swig/swig#927. This avoids calls to
PyEval_SaveThread() and PyEval_RestoreThread(), which can negatively
effect performance. This change only affects module C++ code -- code
that can call Python code like ITKPyUtils does not have threads
enabled.

Release the Python Global Interpreter Lock (GIL) so multi-threaded
Python applications, like Dask, can run in parallel while ITK does heavy
operations.

For more information, see section 32.13 Support for Multithreaded
Applications at

 http://www.swig.org/Doc4.0/Python.html#Python

Also, set `nothreadallow` as suggested in the comments
on swig/swig#927. This avoids calls to
`PyEval_SaveThread()` and `PyEval_RestoreThread()`, which can negatively
effect performance. This change only affects module C++ code -- code
that can call Python code like ITKPyUtils does not have `threads`
enabled.
@thewtex thewtex added the area:Python wrapping Python bindings for a class label Jul 6, 2019
@thewtex thewtex requested a review from hjmjohnson July 6, 2019 20:57
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no objections 😄

@blowekamp
Copy link
Member

The GIL should be reacquired when calling Python call backs. See SimpleITK for an example:
https://github.com/SimpleITK/SimpleITK/blob/master/Wrapping/Python/sitkPyCommand.cxx

thewtex added 2 commits July 8, 2019 17:11
Obtain and return the Python Global Interpreter Lock state before and
after running Python commands to ensure the correct interpreter when
Python is running threads.

Based on sitkPyCommand.cxx.

See also: https://docs.python.org/3/c-api/init.html#non-python-created-threads
@thewtex
Copy link
Member Author

thewtex commented Jul 8, 2019

@blowekamp thanks for the pointer! Added in 3eb1f6a

@blowekamp
Copy link
Member

LGTM

@thewtex thewtex merged commit fdfb6e8 into InsightSoftwareConsortium:release Jul 12, 2019
@thewtex thewtex deleted the release-gil branch July 12, 2019 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Python wrapping Python bindings for a class

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants