@@ -293,24 +293,27 @@ public void TestImportScopeByName()
293293 [ Test ]
294294 public void TestVariables ( )
295295 {
296- ( ps . Variables ( ) as dynamic ) [ "ee" ] = new PyInt ( 200 ) ;
297- var a0 = ps . Get < int > ( "ee" ) ;
298- Assert . AreEqual ( 200 , a0 ) ;
296+ using ( Py . GIL ( ) )
297+ {
298+ ( ps . Variables ( ) as dynamic ) [ "ee" ] = new PyInt ( 200 ) ;
299+ var a0 = ps . Get < int > ( "ee" ) ;
300+ Assert . AreEqual ( 200 , a0 ) ;
299301
300- ps . Exec ( "locals()['ee'] = 210" ) ;
301- var a1 = ps . Get < int > ( "ee" ) ;
302- Assert . AreEqual ( 210 , a1 ) ;
302+ ps . Exec ( "locals()['ee'] = 210" ) ;
303+ var a1 = ps . Get < int > ( "ee" ) ;
304+ Assert . AreEqual ( 210 , a1 ) ;
303305
304- ps . Exec ( "globals()['ee'] = 220" ) ;
305- var a2 = ps . Get < int > ( "ee" ) ;
306- Assert . AreEqual ( 220 , a2 ) ;
306+ ps . Exec ( "globals()['ee'] = 220" ) ;
307+ var a2 = ps . Get < int > ( "ee" ) ;
308+ Assert . AreEqual ( 220 , a2 ) ;
307309
308- using ( var item = ps . Variables ( ) )
309- {
310- item [ "ee" ] = new PyInt ( 230 ) ;
310+ using ( var item = ps . Variables ( ) )
311+ {
312+ item [ "ee" ] = new PyInt ( 230 ) ;
313+ }
314+ var a3 = ps . Get < int > ( "ee" ) ;
315+ Assert . AreEqual ( 230 , a3 ) ;
311316 }
312- var a3 = ps . Get < int > ( "ee" ) ;
313- Assert . AreEqual ( 230 , a3 ) ;
314317 }
315318
316319 /// <summary>
@@ -324,49 +327,55 @@ public void TestThread()
324327 //should be removed.
325328 dynamic _ps = ps ;
326329 var ts = PythonEngine . BeginAllowThreads ( ) ;
327- using ( Py . GIL ( ) )
328- {
329- _ps . res = 0 ;
330- _ps . bb = 100 ;
331- _ps . th_cnt = 0 ;
332- //add function to the scope
333- //can be call many times, more efficient than ast
334- ps . Exec (
335- "def update():\n " +
336- " global res, th_cnt\n " +
337- " res += bb + 1\n " +
338- " th_cnt += 1\n "
339- ) ;
340- }
341- int th_cnt = 3 ;
342- for ( int i = 0 ; i < th_cnt ; i ++ )
330+ try
343331 {
344- System . Threading . Thread th = new System . Threading . Thread ( ( ) =>
332+ using ( Py . GIL ( ) )
333+ {
334+ _ps . res = 0 ;
335+ _ps . bb = 100 ;
336+ _ps . th_cnt = 0 ;
337+ //add function to the scope
338+ //can be call many times, more efficient than ast
339+ ps . Exec (
340+ "def update():\n " +
341+ " global res, th_cnt\n " +
342+ " res += bb + 1\n " +
343+ " th_cnt += 1\n "
344+ ) ;
345+ }
346+ int th_cnt = 3 ;
347+ for ( int i = 0 ; i < th_cnt ; i ++ )
348+ {
349+ System . Threading . Thread th = new System . Threading . Thread ( ( ) =>
350+ {
351+ using ( Py . GIL ( ) )
352+ {
353+ //ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly
354+ _ps . update ( ) ;
355+ }
356+ } ) ;
357+ th . Start ( ) ;
358+ }
359+ //equivalent to Thread.Join, make the main thread join the GIL competition
360+ int cnt = 0 ;
361+ while ( cnt != th_cnt )
345362 {
346363 using ( Py . GIL ( ) )
347364 {
348- //ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly
349- _ps . update ( ) ;
365+ cnt = ps . Get < int > ( "th_cnt" ) ;
350366 }
351- } ) ;
352- th . Start ( ) ;
353- }
354- //equivalent to Thread.Join, make the main thread join the GIL competition
355- int cnt = 0 ;
356- while ( cnt != th_cnt )
357- {
367+ System . Threading . Thread . Sleep ( 10 ) ;
368+ }
358369 using ( Py . GIL ( ) )
359370 {
360- cnt = ps . Get < int > ( "th_cnt" ) ;
371+ var result = ps . Get < int > ( "res" ) ;
372+ Assert . AreEqual ( 101 * th_cnt , result ) ;
361373 }
362- System . Threading . Thread . Sleep ( 10 ) ;
363374 }
364- using ( Py . GIL ( ) )
375+ finally
365376 {
366- var result = ps . Get < int > ( "res" ) ;
367- Assert . AreEqual ( 101 * th_cnt , result ) ;
377+ PythonEngine . EndAllowThreads ( ts ) ;
368378 }
369- PythonEngine . EndAllowThreads ( ts ) ;
370379 }
371380 }
372381}
0 commit comments