Usually I'd like to run my python code in linux as the following:
nohup python test.py > nohup.txt 2>&1 &
in the file test.py, I often use print to print out some messages to stdout. But actually I have to wait very long time then could see the messages were printed out to nohup.txt. How can I make it print out quickly.
You could call flush on stdout. If it is possible and practical for you to adjust your code to flush your buffers after the print call, in test.py:
from sys import stdout
from time import sleep
def log():
while True:
print('Test log message')
# flush your buffer
stdout.flush()
sleep(1)
While running this logging test, you can check the nohup.txt file and see the messages being printed out in realtime.
just make sure you have coreutils installed
stdbuf -oL nohup python test.py > nohup.txt 2>&1 &
this just sets buffering to off for this command ... you should see immediate output
(you might need nohup before stdbuf ... im not sure exactly)
alternatively ... just put at the top of test.py
import sys
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
Related
After searching around, I defined a function to execute command like in terminal:
import shlex
import subprocess
def execute_cmd(cmd):
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in iter(p.stdout.readline, b''): # b'' here for python3
sys.stdout.write(line.decode(sys.stdout.encoding))
error = p.stderr.read().decode()
if error:
raise Exception(error)
It works fine(output is realtime), when i
execute_cmd('ping -c 5 www.google.com')
However, when i use execute_cmd to run a python script, the output will print out until the process is done.
execute_cmd('python test.py')
script: test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
print('hello')
time.sleep(2)
print('hello, again')
How can i fix it? thanks!
Sorry for not explaining why 'catch the stdout and then write it to stdout again'. Here i really want to do is catching script outputs to logger, the logger output them to screen(StreamHandler) and log file(FileHandler). I builded and tested the logger part, now the 'execute' part. And ignore the stdout= parameter seems not work.
pipeline:
setup logger;
redirect STDOUT, STDERR to logger;
execute scripts;
Because of step 2, if i ignore stdout= parameter, the outputs of scripts will still output to STDOUT, and will not log in file.
Maybe i can set stdout= to logger?
This is a common problem of the underlying output system, notably on Linux or other Unix-like systems. The io library is smart enough to flush output on each \n when it detects that output is directed to a terminal. But this automatic flush does not occur when output is redirected to a file or a pipe, probably for performance reasons. It is not really a problem when only the data matters, but it leads to weird behaviour when timing matters too.
Unfortunately I know no simple way to fix it from the caller program(*). The only possibility is to have the callee force flushing on each line/block or to use unbuffered output:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import sys
print('hello')
sys.stdout.flush()
time.sleep(2)
print('hello, again')
(*) The bullet proof way would be to use a pseudo-terminal. The caller controls the master side and passes the client side to the callee. The library will detect a terminal and will automatically flushes on each line. But it is no longer portable outside the Unix world and is not really a simple way.
I have a problem forwarding the stdout of a subprocess to stdout of the current process.
This is my MWE caller code (runner.py):
import sys
import subprocess
import time
p = subprocess.Popen([sys.executable, "test.py"], stdout=sys.stdout)
time.sleep(10)
p.terminate()
and here is the content of the callee test.py:
import time
while True:
time.sleep(1)
print "Heartbeat"
The following will work and print all the heartbeats to the console:
python runner.py
However, the following does not work, the output text file remains empty (using Python 2.7):
python runner.py > test.txt
What do I have to do?
When the standard output is a TTY (a terminal), sys.stdout is line-buffered by default: each line you print is immediately written to the TTY.
But when the standard output is a file, then sys.stdout is block-buffered: data is written to the file only when a certain amount of data gets printed. By using p.terminate(), you are killing the process before the buffer is flushed.
Use sys.stdout.flush() after your print and you'll be fine:
import sys
import time
while True:
time.sleep(1)
print "Heartbeat"
sys.stdout.flush()
If you were using Python 3, you could also use the flush argument of the print function, like this:
import time
while True:
time.sleep(1)
print("Heartbeat", flush=True)
Alternatively, you can also set up a handler for SIGTERM to ensure that the buffer is flushed when p.terminate() gets called:
import signal
signal.signal(signal.SIGTERM, sys.stdout.flush)
It is possible to force flushes by doing sys.stdout.flush() after each print, but this would quickly become cumbersome. Since you know you're running Python, it is possible to force Python to unbuffered mode - either with -u switch or PYTHONUNBUFFERED environment variable:
p = subprocess.Popen([sys.executable, '-u', 'test.py'], stdout=sys.stdout)
or
import os
# force all future python processes to be unbuffered
os.environ['PYTHONUNBUFFERED'] = '1'
p = subprocess.Popen([sys.executable, 'test.py'])
You don't need to pass stdout=sys.stdout unless sys.stdout uses a different file descriptor than the one that python executable has started with. C stdout fd is inherited by default: you don't need to do anything in order for the child process to inherit it.
As #Andrea Corbellini said, if the output is redirected to a file then python uses a block-buffering mode and "Heartbeat"*10 (usually) is too small to overflow the buffer.
I would expect that python flushes its internal stdout buffers on exit but it doesn't do it on SIGTERM signal (generated by the .terminate() call).
To allow the child process to exit gracefully, use SIGINT (Ctrl+C) instead of p.terminate():
p.send_signal(signal.SIGINT)
In that case test.py will flush the buffers and you'll see the output in test.txt file. Either discard stderr or catch KeyboardInterrupt exception in the child.
If you want to see the output while the child process is still running then run python -u, to disable buffering or set PYTHONUNBUFFERED envvar to change the behavior of all affected python processes as #Antti Haapala suggested.
Note: your parent process may also buffer the output. If you don't flush the buffer in time then the output printed before test.py is even started may appear after its output in the test.txt file. The buffers in the parent and the child processes are independent. Either flush buffers manually or make sure that an appropriate buffering mode is used in each process. See Disable output buffering
Write a simple python script test.py:
import time
print "begin"
time.sleep(10)
print "stop"
In bash, run
python test.py > log.txt
What I observe is that both "begin" and "stop" appear in log.txt at the same time, after 10 seconds.
Is this expected behavior?
Have you tried calling python with the -u option, which "forces stdin, stdout and stderr to be totally unbuffered" =>
python -u test.py > log.txt
You need to flush the buffer after printing.
import sys
import time
print "begin"
sys.stdout.flush()
time.sleep(10)
print "end"
sys.stdout.flush()
Or in Python 3:
# This can also be done in Python 2.6+ with
# from __future__ import print_function
import time
print("begin", flush=True)
time.sleep(10)
print("end", flush=True)
This is because the actual writing happens only when a buffer is full, or when the file is closed. When the program ends (after 10s), the buffer is emptied, the data gets written and the file closed.
I'm using Python 2.7.6 and IDLE on Windows 7.
I have 2 Python scripts:
script.py:
import subprocess, os, sys
print("hello 1")
mypath = os.path.abspath(__file__)
mydir = os.path.dirname(mypath)
start = os.path.join(mydir, "script2.py")
subprocess.call([sys.executable, start, "param"])
print("bye 1")
and script2.py that is being called by the previous script:
import sys
print "hello 2"
print (sys.argv[1])
print "bye 2"
If I run script.py with cmd.exe shell I get the expected result:
C:\tests>python ./script.py
hello 1
hello 2
param
bye 2
bye 1
But if I open script.py with the IDLE editor and run it with F5 I get this result:
>>> ================================ RESTART ================================
>>>
hello 1
bye 1
>>>
Why is the sub script not writing to the IDLE Python shell?
You're running the subprocess without providing any stdout or stderr.
When run in a terminal, the subprocess will inherit your stdout and stderr, so anything it prints will show up intermingled with your output.
When run in IDLE, the subprocess will also inherit your stdout and stderr, but those don't go anywhere. IDLE intercepts the Python-level wrappers sys.stdout and sys.stderr,* so anything you print to them from within Python will end up in the GUI window, but anything that goes to real stdout or stderr—like the output of any subprocess you run that inherits your streams—just goes nowhere.**
The simplest fix is to capture the stdout and stderr from the subprocess and print them yourself. For example:
out = subprocess.check_output([sys.executable, start, "param"],
stderr=subprocess.STDOUT)
print out
* IDLE is more complicated than it looks. It's actually running separate processes for the GUI window and for running your code, communicating over a socket. The sys.stdout (and likewise for the others) that IDLE provides for your script isn't a file object, it's a custom file-like object that redirects each write to the GUI process via remote procedure call over the socket.
** Actually, if you launched IDLE from a terminal rather than by double-clicking its icon, the subprocess's output may end up there. I'm not sure how it works on Windows. But at any rate, that isn't helpful to you.
I verified that abamert's change works in 2.7, on Win7, with Idle started normally from the icon. The slight glitch is that 'print out' inserts an extra blank line. This is easily changed by making print a function with a future import and use of the end parameter.
from __future__ import print_function
...
print(out, end='')
With Python 3, there is an additional issue that 'out' is bytes instead of str, so that it prints as
b'hello 2\r\nparam\r\nbye 2\r\n'
Since your output is all ascii, this can be fixed by changing the print call to
print(out.decode(), end='')
The resulting program works identically in 2.7 and 3.x.
I have a python script test.py:
print "first"
import os
os.system("echo second")
On linux command line I execute
python test.py
which returns:
first
second
I then execute
python test.py > test.out; cat test.out
which returns
second
first
What about redirecting the output makes the os.system call print before the print statement??
When you're outputting to a pipe, Python buffers your output that's written to sys.stdout and outputs it after a flush, or after it's overflown, or upon closing (when the program exits). While it'll buffer the print calls, system calls output directly into stdout and their output won't be buffered. That's why you're seeing such precedence. To avoid that, use python -u:
python -u test.py > test.out; cat test.out
See more info here.
EDIT: explanation on when the buffer will be flushed.
Another way to prevent the os buffering is to flush the output after the first print:
#!/usr/bin/env python
import sys
print "first"
sys.stdout.flush()
import os
os.system("echo second")
When the output of the python script is a tty, its output is line buffered. When the output is a regular file, the output is block buffered.