I’m absolutely bewildered by this issue. Let me give a quick run down of the program I’m making:
I’m building a harness that takes an indefinite amount of python scripts, runs the scripts, parses the StdOut/Err, write those results to a database.
I’ve been building it from the bottom up and now I need to piece all the pieces together. However, I’ve been having issues debugging. I usually don’t make console applications, yet for the purposes of what I’m doing, there is no alternative (namely, my objective is to make a console application that takes the python scripts as arguments).
So, inside my namespace I have two main classes that I’ve separated into two different files for clarity’s sake. All the functions within are static so it’s really just an organizational thing. The two files are “Harness” and “Query” respective to the python running output capturing app I wrote and the database reporting app I wrote. Now I’m trying to have the results of Harness go into Query. So that the code is easier to maintain by my peers I am trying to keep them in separate files so that it is more easily digestable.
The problem is that when I launch my command line and navigate the executable and run it passing in a sample python script as an argument, I only get the console output from the Query half of the application. I’ve put some Console.WriteLines in the main(string[] args) function within the Harness file but these never get written. Only the code inside the second Query class is executed–sort of.
Here’s where stuff really begins to confuse me, one of the functions inside my Harness half of the app writes to a log text file that time stamps the run and should report the results. This text file is still being written to. I then thought about having my sample py file output to a text file as my redirect std out was not working apparently. Sure enough, the python opened that text file and wrote to it, suggesting to me that it did indeed run that code.
Yet the function these logging calls are in contains Console.WriteLines that don’t output to console. The only time Console Output is working, is in the second half of the app.
If anyone could shed some light on these issues, I’d really dig it. A lot of the code I have contains some sensitive data so I’d rather not just give a big dump of it, there’s really no specific area of the code that I feel is relevant to the issue–admittedly I don’t know for sure. Let me know what you need from me and I’ll do my best.
List<string> retList = new List<string>();
// Loads Python
Process pr = new Process();
pr.StartInfo.FileName = @"C:\Python27\python.exe";
pr.StartInfo.Arguments = script;
Console.WriteLine(script);
//pr.StartInfo.Arguments = @"C:\PythonProj\hello.py";
pr.StartInfo.UseShellExecute = false;
pr.StartInfo.RedirectStandardOutput = true;
pr.StartInfo.RedirectStandardError = true;
// Runs Test Script
pr.Start();
string output = pr.StandardError.ReadToEnd();
pr.WaitForExit();
Console.WriteLine(output);
Well, the first problem, as I would have expected, is that you have possible deadlock conditions here. I don’t see where you’re reading from standard output at all, and since you’re redirecting both output and error you need to be careful about how you read from them so that you don’t deadlock. The MSDN pages for the redirect properties in Process discuss this issue, and have code examples that should be sufficient for your purposes.
If the problem is that you aren’t seeing the output of the process in your console, so far I don’t see where it should be written (or read from the Process for that matter). Did you omit it for brevity? If so, please include it.