Skip to content

System.AccessViolationException occurs when tried to run a python file multiple times. #1449

@murtazabasu

Description

@murtazabasu

Environment

Environment
Pythonnet version: Runtime Version v4.0.30319
Python version: conda (3.7)
Operating System: Windows 10
.NET Runtime: 4.6.1

Details

Hello, I need to run a python file multiple times . I have a server - client interface where multiple client should be able to execute run a python file. one after the other.

In the below code:

I try to execute a python file two times. I notice that the first time it runs perfectly but for the second time it throws an AccessViolationException saying it Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

class Program
   {
       static void Main(string[] args)
       {
           for (int i = 0; i <= 2; i++)
           {
               connectPython();
               i += 1;
           }
           Console.ReadKey();
       }
       static void connectPython()
       {
           IPythonConnector python = new PythonConnector();
           python.EstablishConnection();
           try
           {
               dynamic mainFile;
               using (Py.GIL())
               {
                   mainFile = Py.Import("main");
               }
               dynamic running = mainFile.main();
           }
           finally
           {
               PythonEngine.Shutdown();
           }
       }

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions