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

Skip to content

Commit dec39c7

Browse files
committed
remove GIL and rebased
1 parent ceaaef0 commit dec39c7

File tree

2 files changed

+266
-213
lines changed

2 files changed

+266
-213
lines changed

src/embed_tests/pyscope.cs

Lines changed: 105 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ public class PyScopeTest
1111
[SetUp]
1212
public void SetUp()
1313
{
14-
ps = Py.Session("test");
14+
using (Py.GIL())
15+
{
16+
ps = Py.Scope("test");
17+
}
1518
}
1619

1720
[TearDown]
1821
public void Dispose()
1922
{
20-
ps.Dispose();
23+
using (Py.GIL())
24+
{
25+
ps.Dispose();
26+
ps = null;
27+
}
2128
}
2229

2330
/// <summary>
@@ -26,9 +33,12 @@ public void Dispose()
2633
[Test]
2734
public void TestEval()
2835
{
29-
ps.SetVariable("a", 1);
30-
var result = ps.Eval<int>("a + 2");
31-
Assert.AreEqual(result, 3);
36+
using (Py.GIL())
37+
{
38+
ps.SetVariable("a", 1);
39+
var result = ps.Eval<int>("a + 2");
40+
Assert.AreEqual(result, 3);
41+
}
3242
}
3343

3444
/// <summary>
@@ -37,11 +47,14 @@ public void TestEval()
3747
[Test]
3848
public void TestExec()
3949
{
40-
ps.SetVariable("bb", 100); //declare a global variable
41-
ps.SetVariable("cc", 10); //declare a local variable
42-
ps.Exec("aa = bb + cc + 3");
43-
var result = ps.GetVariable<int>("aa");
44-
Assert.AreEqual(result, 113);
50+
using (Py.GIL())
51+
{
52+
ps.SetVariable("bb", 100); //declare a global variable
53+
ps.SetVariable("cc", 10); //declare a local variable
54+
ps.Exec("aa = bb + cc + 3");
55+
var result = ps.GetVariable<int>("aa");
56+
Assert.AreEqual(result, 113);
57+
}
4558
}
4659

4760
/// <summary>
@@ -51,11 +64,14 @@ public void TestExec()
5164
[Test]
5265
public void TestCompileExpression()
5366
{
54-
ps.SetVariable("bb", 100); //declare a global variable
55-
ps.SetVariable("cc", 10); //declare a local variable
56-
PyObject script = ps.Compile("bb + cc + 3", "", RunFlagType.Eval);
57-
var result = ps.Execute<int>(script);
58-
Assert.AreEqual(result, 113);
67+
using (Py.GIL())
68+
{
69+
ps.SetVariable("bb", 100); //declare a global variable
70+
ps.SetVariable("cc", 10); //declare a local variable
71+
PyObject script = ps.Compile("bb + cc + 3", "", RunFlagType.Eval);
72+
var result = ps.Execute<int>(script);
73+
Assert.AreEqual(result, 113);
74+
}
5975
}
6076

6177
/// <summary>
@@ -66,12 +82,15 @@ public void TestCompileExpression()
6682
[Test]
6783
public void TestCompileStatements()
6884
{
69-
ps.SetVariable("bb", 100); //declare a global variable
70-
ps.SetVariable("cc", 10); //declare a local variable
71-
PyObject script = ps.Compile("aa = bb + cc + 3", "", RunFlagType.File);
72-
ps.Execute(script);
73-
var result = ps.GetVariable<int>("aa");
74-
Assert.AreEqual(result, 113);
85+
using (Py.GIL())
86+
{
87+
ps.SetVariable("bb", 100); //declare a global variable
88+
ps.SetVariable("cc", 10); //declare a local variable
89+
PyObject script = ps.Compile("aa = bb + cc + 3", "", RunFlagType.File);
90+
ps.Execute(script);
91+
var result = ps.GetVariable<int>("aa");
92+
Assert.AreEqual(result, 113);
93+
}
7594
}
7695

7796
/// <summary>
@@ -80,16 +99,19 @@ public void TestCompileStatements()
8099
[Test]
81100
public void TestSubScope()
82101
{
83-
ps.SetVariable("bb", 100); //declare a global variable
84-
ps.SetVariable("cc", 10); //declare a local variable
102+
using (Py.GIL())
103+
{
104+
ps.SetVariable("bb", 100); //declare a global variable
105+
ps.SetVariable("cc", 10); //declare a local variable
85106

86-
PyScope scope = ps.SubScope();
87-
scope.Exec("aa = bb + cc + 3");
88-
var result = scope.GetVariable<int>("aa");
89-
Assert.AreEqual(result, 113); //
90-
scope.Dispose();
107+
PyScope scope = ps.SubScope();
108+
scope.Exec("aa = bb + cc + 3");
109+
var result = scope.GetVariable<int>("aa");
110+
Assert.AreEqual(result, 113); //
111+
scope.Dispose();
91112

92-
Assert.IsFalse(ps.ContainsVariable("aa"));
113+
Assert.IsFalse(ps.ContainsVariable("aa"));
114+
}
93115
}
94116

95117
/// <summary>
@@ -99,14 +121,17 @@ public void TestSubScope()
99121
[Test]
100122
public void TestImport()
101123
{
102-
dynamic sys = ps.Import("sys");
103-
Assert.IsTrue(ps.ContainsVariable("sys"));
104-
105-
ps.Exec("sys.attr1 = 2");
106-
var value1 = ps.Eval<int>("sys.attr1");
107-
var value2 = (int)sys.attr1.AsManagedObject(typeof(int));
108-
Assert.AreEqual(value1, 2);
109-
Assert.AreEqual(value2, 2);
124+
using (Py.GIL())
125+
{
126+
dynamic sys = ps.Import("sys");
127+
Assert.IsTrue(ps.ContainsVariable("sys"));
128+
129+
ps.Exec("sys.attr1 = 2");
130+
var value1 = ps.Eval<int>("sys.attr1");
131+
var value2 = (int)sys.attr1.AsManagedObject(typeof(int));
132+
Assert.AreEqual(value1, 2);
133+
Assert.AreEqual(value2, 2);
134+
}
110135
}
111136

112137
/// <summary>
@@ -116,28 +141,59 @@ public void TestImport()
116141
[Test]
117142
public void TestImportAs()
118143
{
119-
ps.ImportAs("sys", "sys1");
120-
Assert.IsTrue(ps.ContainsVariable("sys1"));
144+
using (Py.GIL())
145+
{
146+
ps.ImportAs("sys", "sys1");
147+
Assert.IsTrue(ps.ContainsVariable("sys1"));
148+
}
121149
}
122150

123151
/// <summary>
124152
/// Suspend the Session, and reuse it later.
125153
/// </summary>
126154
[Test]
127-
public void TestSuspend()
155+
public void TestThread()
128156
{
129-
ps.SetVariable("bb", 100);
130-
ps.SetVariable("cc", 10);
131-
ps.Suspend();
132-
157+
//I open an proposal here https://github.com/pythonnet/pythonnet/pull/419
158+
//after it merged, the BeginAllowThreads statement blow and the last EndAllowThreads statement
159+
//should be removed.
160+
var ts = PythonEngine.BeginAllowThreads();
133161
using (Py.GIL())
134162
{
135-
PythonEngine.RunSimpleString("import sys;");
163+
ps.SetVariable("res", 0);
164+
ps.SetVariable("bb", 100);
165+
ps.SetVariable("th_cnt", 0);
136166
}
137-
138-
ps.Exec("aa = bb + cc + 3");
139-
var result = ps.GetVariable<int>("aa");
140-
Assert.AreEqual(result, 113);
167+
int th_cnt = 3;
168+
for (int i =0; i< th_cnt; i++)
169+
{
170+
System.Threading.Thread th = new System.Threading.Thread(()=>
171+
{
172+
using (Py.GIL())
173+
{
174+
ps.Exec(
175+
"res += bb + 1\n" +
176+
"th_cnt += 1\n");
177+
}
178+
});
179+
th.Start();
180+
}
181+
//do not use Thread.Join to make this test more complicate
182+
int cnt = 0;
183+
while(cnt != th_cnt)
184+
{
185+
using (Py.GIL())
186+
{
187+
cnt = ps.GetVariable<int>("th_cnt");
188+
}
189+
System.Threading.Thread.Sleep(10);
190+
}
191+
using (Py.GIL())
192+
{
193+
var result = ps.GetVariable<int>("res");
194+
Assert.AreEqual(101* th_cnt, result);
195+
}
196+
PythonEngine.EndAllowThreads(ts);
141197
}
142198
}
143199
}

0 commit comments

Comments
 (0)