I'm currently working with piece of code that uses a script that gets real time data and print it onto my screen.
When I run the script (in which I don't have the access to view) in python console, using:
>>>> myproc = subprocess.Popen('command')
...print....
The output is perfectly printed onto my screen when the data come.
However, if I change it to
>>>> myproc = subprocess.Popen('command', stdout = subprocess.PIPE, stderr = subprocess.PIPE)`
>>>> while True:
>>>> print(myproc.stdout.readline())
... perfectly no print....
there is zero output (I have also checked the stderr, nothing)
I guess the problem here is that i set stdout
to subprocess.PIPE
, and I should probably change that.
However, since i need to store the data that come from myproc
, and using stdout
is so far the only method that i know that allows me to do that, I'm running out of ideas on how to change my code.
Please tell me what can I do here, and also please tell me why did this happen.
Thanks
EDIT: My script listens to real time data, so it will keep on running until I manually interrupt it. I want prints before it actually terminates.
2nd EDIT :
I found out where the missing out put went... I have to change it to
myproc = subprocess.Popen('command', stdout=sys.stdout)
and then use
While True:
if myproc.stdout is not None:
print(myproc.stdout)
for the output to show. But this is just super ugly and the output can be extremely unpredictable.. Can anyone maybe provide a better approach to this ugly thing??