@@ -417,9 +417,11 @@ static void RunAssemblyAndUnload(string domainName)
417
417
type . Assembly . FullName ,
418
418
type . FullName ) ;
419
419
420
+ theProxy . Call ( "InitPython" , ShutdownMode . Soft ) ;
420
421
// From now on use the Proxy to call into the new assembly
421
422
theProxy . RunPython ( ) ;
422
423
424
+ theProxy . Call ( "ShutdownPython" ) ;
423
425
Console . WriteLine ( $ "[Program.Main] Before Domain Unload on { domainName } ") ;
424
426
AppDomain . Unload ( domain ) ;
425
427
Console . WriteLine ( $ "[Program.Main] After Domain Unload on { domainName } ") ;
@@ -544,38 +546,25 @@ public static void RunPython()
544
546
AppDomain . CurrentDomain . DomainUnload += OnDomainUnload ;
545
547
string name = AppDomain . CurrentDomain . FriendlyName ;
546
548
Console . WriteLine ( "[{0} in .NET] In PythonRunner.RunPython" , name ) ;
547
- var mode = PythonEngine . DefaultShutdownMode ;
548
- if ( mode == ShutdownMode . Normal )
549
+ using ( Py . GIL ( ) )
549
550
{
550
- mode = ShutdownMode . Soft ;
551
- }
552
- PythonEngine . Initialize ( mode : mode ) ;
553
- try
554
- {
555
- using ( Py . GIL ( ) )
551
+ try
556
552
{
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 ;
574
567
}
575
- }
576
- finally
577
- {
578
- PythonEngine . BeginAllowThreads ( ) ;
579
568
}
580
569
}
581
570
0 commit comments