title: Python Script run from C# leaves Python exe running in background eating memory
body: <p>I am developing a C# ASP.Net Service that constantly reads GPIO on raspberry pis. This reads pins for inputs and also fires pins for outputs. I have the call working but my problem is I call the Python scripts every .5 seconds when needed. e.g the read input will be called every .5 second but the outputs will only fire when required. My problem is that each call starts a new instance of Python.exe and it does not close them when the script is run. I have tried all kinds of exits from the python script as well as some from the C# eg. process.Kill() but nothing is killing the Python exe. Attached is an image of the task manager to show the issue. Below I will add in the code I am using to call the python script and the read input script to show the python script I am calling. I have left some comments in so you can see what I have previously tried. Thanks</p>
  
  <p><a href="https://i.stack.imgur.com/ehQ5O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ehQ5O.png" alt="enter image description here"></a></p>
  
  <p>Code to call Python Script:</p>
  
  <pre><code>String exePath = SystemSettingDB.GetSettingValue("Python Executbale Path");
  string holdPath = SystemSettingDB.GetSettingValue("Door Open Script Path");
  
  var open = new ProcessStartInfo();
  
  open.FileName = exePath;
  
  var controllerIP = _controllerIP;
  
  open.Arguments = $"\"{holdPath}\" \"{controllerIP}\" \"{_pin}\"";
  
  
  open.UseShellExecute = false;
  open.CreateNoWindow = true;
  open.RedirectStandardOutput = true;
  open.RedirectStandardError = true;
  
  var process = Process.Start(open);
  
  
  if (process != null)
  {
       process.Kill();
       return true;
  }
  </code></pre>
  
  <p>Python Script to read Inputs</p>
  
  <pre><code>import sys
  
  import os
  from time import sleep
  
  os.environ["PIGPIO_PORT"] = "8888"
  os.environ["GPIOZERO_PIN_FACTORY"] = "pigpio"
  from gpiozero import LED
  
  os.environ["PIGPIO_ADDR"] = sys.argv[1]
  gpio_pin = sys.argv[2] 
  
  door = LED(gpio_pin)
  
  door._read()
  print(door.value)
  
  sys.exit(0)
  #os._exit(-1)
  </code></pre>