title: Python called via C# not returning value when in infinite loop
body: <p>I'm currently implementing a .Net app, which connects to a Raspberry SenseHat. To do so, I'm using the Python implementation <a href="https://pythonhosted.org/sense-hat/" rel="nofollow noreferrer">https://pythonhosted.org/sense-hat/</a> and call the python scripts via Processes to be as loosely coupled as possible.
  Everything works fine, but I have some problems with the joystick: The example uses an infinite loop in the Python script. My "Joystock.py" script is currently looking like this:</p>
  
  <pre><code>import sys
  try:
     import queue
  except ImportError:
     import Queue as queue
  import threading
  import requests
  
  from sense_hat import SenseHat
  sense = SenseHat()
  
  # Taken from https://stackoverflow.com/questions/48429653/python-returning-values-from-infinite-loop-thread
  def _listen(queue):
    while True:
      event = sense.stick.wait_for_event(emptybuffer=True)
      val = event.action + ":" + event.direction
      queue.put(val)
  
  def listen(params):
    q = queue.Queue()
    t1 = threading.Thread(target=_listen, name=_listen, args=(q,))
    t1.start()
  
    while True:
      value = q.get()
      print(value)
  
  if __name__ == '__main__':
    args = sys.argv
    args.pop(0) # Remove file path
    methodName = args.pop(0) # Pop method name
  
    globals()[methodName](args)
  </code></pre>
  
  <p>The bottom part is to pass the method name and the parameters I'd like to call via arguments.
  My C# call is looking like this:</p>
  
  <pre><code>public void Listen(PythonListeningRequest request)
  {
      var startInfo = _startInfoFactory.CreateForListening(request);
  
      var process = Process.Start(startInfo);
      process.BeginErrorReadLine();
      process.BeginOutputReadLine();
      process.EnableRaisingEvents = true;
      process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =&gt;
      {
          Console.WriteLine("Input: " + e.Data);
      };
  
      process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =&gt;
      {
          Console.WriteLine("Error: " + e.Data);
      };
  }
  </code></pre>
  
  <p>And the definition of the ProcessStartInfo:</p>
  
  <pre><code>public ProcessStartInfo CreateForListening(PythonRequest request)
  {
      return new ProcessStartInfo
      {
          FileName = FindPythonExeFilePath(),
          Arguments = CreateArgumentsString(request),
          UseShellExecute = false,
          RedirectStandardInput = true,
          RedirectStandardOutput = true,
          RedirectStandardError = true,
          CreateNoWindow = true,
          WindowStyle = ProcessWindowStyle.Hidden
      };
  }
  
  private static string CreateArgumentsString(PythonRequest request)
  {
      var sb = new StringBuilder();
      sb.Append(request.FilePath);
      sb.Append(" ");
  
      sb.Append(request.MethodName);
      sb.Append(" ");
  
      foreach (var arg in request.Arguments)
      {
          sb.Append(arg.AsString());
          sb.Append(" ");
      }
  
      var result = sb.ToString();
      return result;
  }
  
  private string FindPythonExeFilePath()
  {
      var possibleFilePaths = new string[]
      {
          @"C:\Users\mlm\AppData\Local\Programs\Python\Python37-32\python.exe",
          @"C:\WINDOWS\py.exe",
          "/usr/bin/python"
      };
  
      var existingPythonPath = possibleFilePaths.FirstOrDefault(fp =&gt; _fileSystem.File.Exists(fp));
      Guard.That(() =&gt; existingPythonPath != null, "No python path found.");
  
      return existingPythonPath;
  }
  </code></pre>
  
  <p>As you can see in the python part, there is a queue used, which I've got from another SO question. Unfortunately, it still doesn't work, as soon as "t1.start()" is in the code, I never get a return value.</p>
  
  <p>Trying the python script manually works fine, so I guess the problem is the Process connection to C#? Unfortuntely, I didn't find anything related to this behavior, has therefore anyone any idea, what could cause this issue?</p>