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

using(Py.Gil()) hangs while calling from other than init thread #109

Closed
zwadar opened this issue Oct 23, 2015 · 2 comments
Closed

using(Py.Gil()) hangs while calling from other than init thread #109

zwadar opened this issue Oct 23, 2015 · 2 comments

Comments

@zwadar
Copy link

zwadar commented Oct 23, 2015

Hi,
I'm using python.net in C# app and mainly calling python from worker threads. However it always hangs when trying to acquirelock from different thread than thread that did first initialization.
I'm not familiar with exact workflow with GIL, but for me, adding BeginAllowThreads() and EndAllowThreads() to GILState class helped, but I'm not sure it is proper solution.

 public class GILState : IDisposable
        {
            private IntPtr state;
            private IntPtr thread_state;
            internal GILState()
            {
                thread_state = PythonEngine.BeginAllowThreads();
                state = PythonEngine.AcquireLock();
            }
            public void Dispose()
            {
                PythonEngine.ReleaseLock(state);
                GC.SuppressFinalize(this);
                PythonEngine.EndAllowThreads(thread_state);
            }
            ~GILState()
            {
                Dispose();
            }
        }
@den-run-ai
Copy link
Contributor

Someone tried this before:
https://mail.python.org/pipermail/pythondotnet/2010-December/001058.html

@tonyroberts
Copy link
Contributor

The main thread will hold the GIL after initialization until you explicitly release it by calling PythonEngine.BeginAllowThreads() from the main thread (not from your background thread). This is how python threading works, it's not specific to pythonnet.

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

No branches or pull requests

3 participants