Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Releasing GIL in long-running C# method causes "Fatal Python error: PyEval_SaveThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)"#2209

sherryyshi started this conversation inGeneral
Discussion options

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.9.13
  • Operating System: Windows 11
  • .NET Runtime: .NET Core 3.1

Details

I'm trying to prevent a long running method in C# from blocking execution in Python. I followed the guidance onhttps://github.com/pythonnet/pythonnet/wiki/Threading but I am getting a crash.

importpythonnetpythonnet.load('coreclr')importosbin_path=os.path.normpath(os.path.dirname(os.path.abspath(__file__))+"/../LongRunningTask/bin/Debug/netcoreapp3.1")importclrclr.AddReference(os.path.join(bin_path,"LongRunningTask"))clr.AddReference("System")fromLongRunningTaskimportLongRunningTaskfromthreadingimportThread,Timert1=Thread(target=LongRunningTask.Run)t1.daemon=Truet1.start()
usingSystem.Threading;usingPython.Runtime;namespaceLongRunningTask{publicclassLongRunningTask{publicstaticvoidRun(){varstate=PythonEngine.BeginAllowThreads();Thread.Sleep(1000);PythonEngine.EndAllowThreads(state);}}}
  • If there was a crash, please include the traceback here.
Fatal Python error: PyGILState_Release: thread state 000001D4A5CA2A40 must be current when releasingPython runtime state: initializedThread 0x00001c08 (most recent call first):  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 917 in run  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 980 in _bootstrap_inner  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 937 in _bootstrapCurrent thread 0x00005f6c (most recent call first):  File "C:\Users\shersh\AppData\Roaming\Python\Python39\site-packages\clr_loader\types.py", line 64 in __call__  File "C:\Users\shersh\AppData\Roaming\Python\Python39\site-packages\pythonnet\__init__.py", line 157 in unloadFatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.   at Python.Runtime.Runtime.PyGILState_Release(Python.Runtime.Native.PyGILState)   at Python.Runtime.PythonEngine.ReleaseLock(Python.Runtime.Native.PyGILState)   at Python.Runtime.Py+GILState.Dispose()   at Python.Runtime.Loader.Shutdown(IntPtr, Int32)
You must be logged in to vote

Replies: 2 comments 3 replies

Comment options

When Python calls C# GIL is not held in the first place, so no need to release anything.

You must be logged in to vote
1 reply
@sherryyshi
Comment options

I see, thanks.

Comment options

Hi@lostmsu. I'm confused by your comment:

"When Python calls C# GIL is not held in the first place, so no need to release anything"

This seems to contradict this pagehttps://github.com/pythonnet/pythonnet/wiki/Threading which says:

"If you are calling C# from Python, and the C# code performs a long-running operation (e.g. computation, I/O, sleep, acquiring a lock, ...), release the GIL via PythonEngine.BeginAllowThreads(). Remember to restore it before returning control to Python"

The same question is here:#2424

You must be logged in to vote
2 replies
@lostmsu
Comment options

This might be confusing. I believe by default now GIL is not held when .NET code is called from Python. So actually in order to work with any Python objects you have to call and holdPy.GIL().

The wiki is outdated.

@DareDevilDenis
Comment options

Thanks for the clarification

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
3 participants
@sherryyshi@lostmsu@DareDevilDenis
Converted from issue

This discussion was converted from issue #2208 on August 04, 2023 22:07.


[8]ページ先頭

©2009-2025 Movatter.jp