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

Skip to content

Commit 2b11631

Browse files
committed
make sure the GIL is released in ThreadTest
1 parent 10a2f8f commit 2b11631

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

pythonnet/src/testing/threadtest.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,43 @@ public class ThreadTest {
3939

4040
public static string CallEchoString(string arg) {
4141
IntPtr gs = PythonEngine.AcquireLock();
42-
if (module == null) {
43-
module = PythonEngine.ModuleFromString("tt", testmod);
42+
try {
43+
if (module == null) {
44+
module = PythonEngine.ModuleFromString("tt", testmod);
45+
}
46+
PyObject func = module.GetAttr("echostring");
47+
PyString parg = new PyString(arg);
48+
PyObject temp = func.Invoke(parg);
49+
string result = (string)temp.AsManagedObject(typeof(String));
50+
func.Dispose();
51+
parg.Dispose();
52+
temp.Dispose();
53+
return result;
54+
}
55+
finally {
56+
PythonEngine.ReleaseLock(gs);
4457
}
45-
PyObject func = module.GetAttr("echostring");
46-
PyString parg = new PyString(arg);
47-
PyObject temp = func.Invoke(parg);
48-
string result = (string)temp.AsManagedObject(typeof(String));
49-
func.Dispose();
50-
parg.Dispose();
51-
temp.Dispose();
52-
PythonEngine.ReleaseLock(gs);
53-
return result;
5458
}
5559

5660
public static string CallEchoString2(string arg) {
5761
IntPtr gs = PythonEngine.AcquireLock();
58-
if (module == null) {
59-
module = PythonEngine.ModuleFromString("tt", testmod);
60-
}
62+
try {
63+
if (module == null) {
64+
module = PythonEngine.ModuleFromString("tt", testmod);
65+
}
6166

62-
PyObject func = module.GetAttr("echostring2");
63-
PyString parg = new PyString(arg);
64-
PyObject temp = func.Invoke(parg);
65-
string result = (string)temp.AsManagedObject(typeof(String));
66-
func.Dispose();
67-
parg.Dispose();
68-
temp.Dispose();
69-
PythonEngine.ReleaseLock(gs);
70-
return result;
67+
PyObject func = module.GetAttr("echostring2");
68+
PyString parg = new PyString(arg);
69+
PyObject temp = func.Invoke(parg);
70+
string result = (string)temp.AsManagedObject(typeof(String));
71+
func.Dispose();
72+
parg.Dispose();
73+
temp.Dispose();
74+
return result;
75+
}
76+
finally {
77+
PythonEngine.ReleaseLock(gs);
78+
}
7179
}
7280

7381

0 commit comments

Comments
 (0)