Why is log.plaintext responding late in Python? [duplicate] - python

This question already has answers here:
Run command with PyQt5 and getting the stdout and stderr
(2 answers)
How to keep PyQt5 responsive when calling gnuplot?
(1 answer)
Closed 1 year ago.
I want to see the immediate execution result of stdout after exe's execution on pyqt. I implemented all the actions I want in Python code, and the rest are linked to pyqt's widget. I want to print out the immediate execution result of exe from pyqt. Though everything outputs normally in the cmd window, while only log.insertPlainText part output shows at the last time after all the cmd execution ends.
I am sure this is not a duplicate question. What I want is not a solution from pyqt, but I want to solve it entirely with Python code. I've tried many different things, but the log is still printed all at once at the end of the cmd execution. The reason I used log.insertPlainText instead of print is, because I want to show the output result in pyqt.
What I already tried:
cmd = [cmd command]
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, universal_newlines=True, bufsize=1)
for line in iter(p.stdout):
log.insertPlainText(line)
p.stdout.close()
p.wait()
and
with Popen([cmd command], stdout=PIPE, bufsize=1,
universal_newlines=True) as p:
for line in p.stdout:
log.insertPlainText(line)

Related

Run tail command as background process in Python [duplicate]

This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed last year.
I have file named test.py and I am trying to run below command as a background process as part of this script
"tail -n0 -f debug.log"
Also, I want this process to end as soon as test.py execution is completed.
However, I can't get this to working. I have tried below code but tail command not exiting even after main script is completed.
I am new Python, can someone help me do this clean way ?
pro = subprocess.Popen(["tail", "-n0", "-f", log_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in pro.stdout:
print(line)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
I used it once before, so I can't remember exactly, but I think I exiting 'subprocess.Popen' using 'with'. I'm not sure, but I recommend giving it a try.
with subprocess.Popen(["tail", "-n0", "-f", log_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as pro:
for line in pro.stdout:
print(line)

Mimic cmd.exe output from C program to Python subprocess.PIPE?

As an add-on to my old question: I have a question. When I run cmd.exe and execute the target program, the output is printed to cmd nicely and it exits with code 0 in the end,
Since the program continuously prints to my cmd.exe stdout, how come I can't mimic that behaviour in Python?
The following code is how I parse lines from my target executable.
res = subprocess.Popen(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1)
with res.stdout:
for line in iter(res.stdout.readline, b''):
print line
res.wait()
The python parsing doesnt even read the same things as cmd.exe does!
It doesnt print the last 5-10 lines (the ones telling me the process is complete).
Do I have to subprocess popen cmd.exe then call the target program? Are there any other alternatives?

Python - read output from long-running subprocess [duplicate]

This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed 6 years ago.
Using the subprocess module (Python 2.7), I'm running a command and attempting to process its output as it runs.
I have code like the following:
process = subprocess.Popen(
['udevadm', 'monitor', '--subsystem=usb', '--property'],
stdout=subprocess.PIPE)
for line in iter(process.stdout.readline, ''):
print(line)
However, the output only gets printed after I Ctrl+C, even if I add sys.stdout.flush() after the print statement.
Why is this happening, and how can I live stream the output from this process?
Notably, this udevadm monitor command is not intended to terminate, so I can't simply wait for the process to terminate and process its output all at once.
I found live output from subprocess command but the approach in the accepted answer did not solve my problem.
You could use unbuffer :
process = subprocess.Popen(
["unbuffer", 'udevadm', 'monitor', '--subsystem=usb', '--property'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
print(line)

subprocess.Popen not printing/running properly [duplicate]

This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed 7 years ago.
I have a script that reads from external sensors (and runs forever), when I run it as ./zwmeter /dev/ttyUSB0 300 it behaves normally and prints output continuously to stdout. I am using bash on Ubuntu. I'd like to execute this command as part of a python script. I have tried:
from subprocess import Popen, PIPE
proc = Popen(['./zwmeter', '/dev/ttyUSB0', '300'], stderr=PIPE, stdout=PIPE)
print proc.communicate()
but I get a program that runs forever without producing any output. I don't care about stderr, only stdout and have tried splitting up the printing but still with no success.
Thank you for any help you can provide!
I think the problem has to do with the process I'm calling not terminating. I found a good work around on this site:
http://eyalarubas.com/python-subproc-nonblock.html

Python: Obtain output using subprocess.call, not Popen [duplicate]

This question already has answers here:
Retrieving the output of subprocess.call() [duplicate]
(7 answers)
Closed 8 years ago.
I have a system call made from a Python script. I would like to have a timer as well as utilizing the output of the call. I was able to do one at a time: Implement a timer using subprocess.call() and retrieve the output using subprocess.Popen(). However, I need both timer and the output result.
Is there any way to achieve this?
Following code give me an Attribute error: 'int' object has no attribute 'stdout', because the subprocess.call output is not the Popen object I need to use.
... Open file here ...
try:
result = subprocess.call(cmd, stdout=subprocess.PIPE, timeout=30)
out = result.stdout.read()
print (out)
except subprocess.TimeoutExpired as e:
print ("Timed out!")
... Write to file here ...
Any help would be appreciated.
In the documentation on subprocess.call() the one of the first things I noticed was:
Note:
Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.
The next thing was the first line in the documentation
Run the command described by args. Wait for command to complete, then return the returncode attribute.
subprocess.call will return the "exit code", an int, generally 0 = success, 1 = something went wrong, etc.
For more infomation on exit codes...http://www.tldp.org/LDP/abs/html/exitcodes.html
Since you want the 'output'from your timer, you might want to revert to
timer_out = subprocess.Popen(command, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
stout, sterror = timer_out.communicate()
...or something like it.

Categories