Skip to content
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

Consolidate python GIL / threads context handling #145

Closed
arahlin opened this issue Feb 27, 2024 · 0 comments · Fixed by #146
Closed

Consolidate python GIL / threads context handling #145

arahlin opened this issue Feb 27, 2024 · 0 comments · Fixed by #146
Assignees

Comments

@arahlin
Copy link
Member

arahlin commented Feb 27, 2024

See potential implementation in #69, which can be split off into a separate PR.

@arahlin arahlin self-assigned this Feb 28, 2024
arahlin added a commit that referenced this issue Mar 1, 2024
This PR creates a new class that simplifies initialization of python threads, as well as acquiring / releasing the Python global interpreter lock in various contexts.

Use cases include:

1. Ensuring that Py_Initialize() is properly called at the beginning of a program that is expected to interact with the python interpreter, and also that Py_Finalize() is called when the program is finished.
2. Ensuring that the current thread state is saved and the GIL released as necessary, e.g. for IO operations, and then the thread state is restored on completion.
3. Ensuring that the GIL is acquired for one-off interaction with the python interpreter, and released when complete.

A G3PythonContext object is used throughout the library code for cases 2 and 3. Case 1 is handled by a G3PythonInterpreter object constructed at the main program/thread level.  If the python interpreter has not been initialized (i.e. the compiled program is expected to be purely in C++), then the library context objects are essentially no-op. If the python interpreter is initialized (e.g. inside a python program or command-line interface), then these library context objects will handle the GIL appropriately.

See the examples/cppexample.cxx C++ program for a simple implementation of the above behavior in a compiled program.

Closes #145.

---------

Co-authored-by: Cosmin Deaconu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant