@@ -417,9 +417,11 @@ static void RunAssemblyAndUnload(string domainName)
417417 type . Assembly . FullName ,
418418 type . FullName ) ;
419419
420+ theProxy . Call ( "InitPython" , ShutdownMode . Soft ) ;
420421 // From now on use the Proxy to call into the new assembly
421422 theProxy . RunPython ( ) ;
422423
424+ theProxy . Call ( "ShutdownPython" ) ;
423425 Console . WriteLine ( $ "[Program.Main] Before Domain Unload on { domainName } ") ;
424426 AppDomain . Unload ( domain ) ;
425427 Console . WriteLine ( $ "[Program.Main] After Domain Unload on { domainName } ") ;
@@ -544,38 +546,25 @@ public static void RunPython()
544546 AppDomain . CurrentDomain . DomainUnload += OnDomainUnload ;
545547 string name = AppDomain . CurrentDomain . FriendlyName ;
546548 Console . WriteLine ( "[{0} in .NET] In PythonRunner.RunPython" , name ) ;
547- var mode = PythonEngine . DefaultShutdownMode ;
548- if ( mode == ShutdownMode . Normal )
549+ using ( Py . GIL ( ) )
549550 {
550- mode = ShutdownMode . Soft ;
551- }
552- PythonEngine . Initialize ( mode : mode ) ;
553- try
554- {
555- using ( Py . GIL ( ) )
551+ try
556552 {
557- try
558- {
559- var pyScript = string . Format ( "import clr\n "
560- + "print('[{0} in python] imported clr')\n "
561- + "clr.AddReference('System')\n "
562- + "print('[{0} in python] allocated a clr object')\n "
563- + "import gc\n "
564- + "gc.collect()\n "
565- + "print('[{0} in python] collected garbage')\n " ,
566- name ) ;
567- PythonEngine . Exec ( pyScript ) ;
568- }
569- catch ( Exception e )
570- {
571- Console . WriteLine ( string . Format ( "[{0} in .NET] Caught exception: {1}" , name , e ) ) ;
572- throw ;
573- }
553+ var pyScript = string . Format ( "import clr\n "
554+ + "print('[{0} in python] imported clr')\n "
555+ + "clr.AddReference('System')\n "
556+ + "print('[{0} in python] allocated a clr object')\n "
557+ + "import gc\n "
558+ + "gc.collect()\n "
559+ + "print('[{0} in python] collected garbage')\n " ,
560+ name ) ;
561+ PythonEngine . Exec ( pyScript ) ;
562+ }
563+ catch ( Exception e )
564+ {
565+ Console . WriteLine ( string . Format ( "[{0} in .NET] Caught exception: {1}" , name , e ) ) ;
566+ throw ;
574567 }
575- }
576- finally
577- {
578- PythonEngine . BeginAllowThreads ( ) ;
579568 }
580569 }
581570
0 commit comments