Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ec9a6c

Browse files
committed
Release the GIL
Addresses comment: #958
1 parent 09f8281 commit 7ec9a6c

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

src/embed_tests/TestRuntime.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,27 @@ public static void PlatformCache()
4040
[Test]
4141
public static void Py_IsInitializedValue()
4242
{
43-
if (Runtime.Runtime.Py_IsInitialized() == 1)
43+
IntPtr state = IntPtr.Zero;
44+
try
4445
{
45-
Runtime.Runtime.PyGILState_Ensure();
46+
if (Runtime.Runtime.Py_IsInitialized() == 1)
47+
{
48+
state = Runtime.Runtime.PyGILState_Ensure();
49+
}
50+
Runtime.Runtime.Py_Finalize();
51+
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
52+
Runtime.Runtime.Py_Initialize();
53+
Assert.AreEqual(1, Runtime.Runtime.Py_IsInitialized());
54+
Runtime.Runtime.Py_Finalize();
55+
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
56+
}
57+
finally
58+
{
59+
if(state != IntPtr.Zero)
60+
{
61+
Runtime.Runtime.PyGILState_Release(state);
62+
}
4663
}
47-
Runtime.Runtime.Py_Finalize();
48-
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
49-
Runtime.Runtime.Py_Initialize();
50-
Assert.AreEqual(1, Runtime.Runtime.Py_IsInitialized());
51-
Runtime.Runtime.Py_Finalize();
52-
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
5364
}
5465

5566
[Test]

0 commit comments

Comments
 (0)