1

I'm trying to redirect that standard output from a Command Line Project I wrote in C# and reading through the data in a Python file.

Currently, the C# application writes data that it reads from a sensor into a CSV file. I have to run the Python file later to get and process the data (has to be done in Python and the data collection has to be done in .NET to use the SDK).

I want to be able to run the C# and the Python projects at the same time and transfer the stream of data directly from the C# to Python project without the use of an intermediate, local file (the CSV).

I've done my own hunting on SO and in the MSDN Documentation. I'm looking at using the ProcessStartInfo.RedirectStandardOutput Property to redirect the console output of the C# application.

I don't yet know how to pick up on the data in this Stream from a Python file. This article was helpful in grasping a better understanding of how to approach it, but I'm still stuck.

I'm also looking at subprocess.Popen.communicate in Python but I'm not yet sure if that would work for what I am asking.

Any help would be appreciated.

  • 2
    Are you calling the C# CLI (compiled I presume?) from within Python? Through `subprocess`? Could you add a little more detail to your question? – DocZerø Aug 02 '16 at 18:06
  • This is a Python question. There's nothing you need to do to your C# project if it's already outputting text to stdout (e.g., using `Console.Writeline()`). *Python* is the one that needs to redirect the stdout stream (assuming it spawns the c# program). In c# redirecting is filled with caveats and there's multiple ways to do it, so, assuming python has similar complexity and caveats, you are probably better off changing your c# program to output to a text file, then read that text file into python (because I assume it's easier than capturing redirected output). – Quantic Aug 02 '16 at 18:11
  • @Kristof I added some more information to the post for clarity. – Karan Narula Aug 02 '16 at 18:17
  • @Quantic Currently, I am doing just that but with a CSV file. I'm sure that modifying the Python would be more complex, but I'm trying to run both the C# and the Python projects at the same time without the use of an intermediate file. Thanks – Karan Narula Aug 02 '16 at 18:17
  • 1
    As long as your C# program `Console.Writeline`'s every line that it also writes to the CSV file, then you are done with the c# program. As for having python capture those lines being output to the console (which goes to stdout), I looked around and found e.g., [this](http://stackoverflow.com/questions/874815/how-do-i-get-real-time-information-back-from-a-subprocess-popen-in-python-2-5), which I found from [here](http://stackoverflow.com/questions/7608883/python-capture-stdout-from-subprocess-line-by-line). As you can see, it's not a simple "one liner" that always works for everything. – Quantic Aug 02 '16 at 18:26
  • 1
    Have you considered using a socket for your cross process communication instead of using stdout? – Clay Ver Valen Aug 02 '16 at 18:39

1 Answers1

0

Thanks to @Quantic for providing some great resources. Using subprocess.Popen, I can run the built .exe of my C# project and redirect the output to my Python file. I am able now to print() to output to the Python console all the information being output to the C# console (Console.WriteLine()).

Python code:

from subprocess import Popen, PIPE, STDOUT

p = Popen('ConsoleDataImporter.exe', stdout = PIPE, stderr = STDOUT, shell = True)

while True:
    line = p.stdout.readline()
    print(line)
    if not line:
        break

This gets the console output of the .NET project line by line as it is created and breaks out of the enclosing while loop upon the project's termination.