Running python script on C# and getting output continuously

前端 未结 1 338
轻奢々
轻奢々 2021-01-16 04:26

I\'m trying to run a python script from C# and I want to get the output line by line and not at the end. I feel like I\'m missing something important, but don\'t know what.

相关标签:
1条回答
  • 2021-01-16 04:56

    change your python code to the following:

    import time
    import sys
    for i in range(5):
        print("Hello World " + str(i))
        sys.stdout.flush()
        time.sleep(1)
    

    or just edit your c# code and use -u switch:

    var cmd = "-u C:/Users/user/Documents/script.py";
    

    When standard output it was being redirected, the event in C# wasn't being raised when a line was written on console because there were no calls to stdout.flush;

    Putting a stdout.flush() statement after each print statement made the events fire as they should and C# now captures the output as it comes.

    Or you could just use -u switch.

    0 讨论(0)
提交回复
热议问题