Skip to content

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

@zwadar

Description

@zwadar

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();
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions