后台运行python程序 遇到缓冲区问题
2014-09-12 17:26
260 查看
From: http://www.iteye.com/topic/867446
环境:linux
一段执行时间很长的程序(用python做hive客户端执行mapreduce) 在linux后台执行,把结果输出到某文件:
Python代码
python xxx.py > log.log&
遇到的问题,程序没报错,文件里却什么都没有,没有输入进去。为什么呢?
于是我首先尝试用:
Java代码
nohup python xxx.py > log.log &
预料之中 还是不行。
于是我做实验:
写了个test.py:
Java代码
import sys,time
from threading import Thread
class worker(Thread):
def run(self):
for x in xrange(0,111):
print x
time.sleep(1)
def run():
worker().start()
if __name__ == '__main__':
run()
每秒打印一次
我直接用python text.py 执行 没问题 每秒输出一个数
但我在后台执行:
Python代码
python test.py > log.log&
还是不行。开始不输出 直到程序执行完,一次性的写到log.log文件了。
为什么呢?
原因可能是python 的print 先写到缓冲区了,还没flush到文件。
于是加了一行“ sys.stdout.flush()” --在每次print后都flush一次。:
Java代码
import sys,time
from threading import Thread
class worker(Thread):
def run(self):
for x in xrange(0,111):
print x
sys.stdout.flush()
time.sleep(1)
def run():
worker().start()
if __name__ == '__main__':
run()
问题解决。
===============================================================================
还可以:python-u
xxx.py > log.log & ,再细看下帮助文档:man python
PYTHON(1) PYTHON(1)
NAME
python - an interpreted, interactive, object-oriented programming language
SYNOPSIS
python [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ] [ -O ]
[ -Q argument ] [ -S ] [ -t ] [ -u ]
[ -v ] [ -V ] [ -W argument ] [ -x ] [ -3 ]
[ -c command | script | - ] [ arguments ]
DESCRIPTION
Python is an interpreted, interactive, object-oriented programming language that combines remarkable power with very clear syntax.
For an introduction to programming in Python you are referred to the Python Tutorial. The Python Library Reference documents built-in
and standard types, constants, functions and modules. Finally, the Python Reference Manual describes the syntax and semantics of the
core language in (perhaps too) much detail. (These documents may be located via the INTERNET RESOURCES below; they may be installed
on your system as well.)
Python’s basic power can be extended with your own modules written in C or C++. On most systems such modules may be dynamically
loaded. Python is also adaptable as an extension language for existing applications. See the internal documentation for hints.
Documentation for installed Python modules and packages can be viewed by running the pydoc program.
COMMAND LINE OPTIONS
-c command
Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments
to the command).
-d Turn on parser debugging output (for wizards only, depending on compilation options).
-E Ignore environment variables like PYTHONPATH and PYTHONHOME that modify the behavior of the interpreter.
-h Prints the usage for the interpreter executable and exits.
-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the
command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a
script raises an exception.
-m module-name
Searches sys.path for the named module and runs the corresponding .py file as a script.
-u Force
stdin, stdout and stderr to be totallyunbuffered.
On systems where it matters, also put stdin, stdout and stderr in
binary mode. Note that there is internal buffering in xreadlines(), readlines() and file-object
iterators ("for line in
sys.stdin") which is not influenced by this option. To work around this, you will want to use "sys.stdin.readline()" inside a
"while 1:" loop.
-v Print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded.
When given twice, print a message for each file that is checked for when searching for a module. Also provides information on
module cleanup at exit.
-V Prints the Python version number of the executable and exits.
环境:linux
一段执行时间很长的程序(用python做hive客户端执行mapreduce) 在linux后台执行,把结果输出到某文件:
Python代码
python xxx.py > log.log&
遇到的问题,程序没报错,文件里却什么都没有,没有输入进去。为什么呢?
于是我首先尝试用:
Java代码
nohup python xxx.py > log.log &
预料之中 还是不行。
于是我做实验:
写了个test.py:
Java代码
import sys,time
from threading import Thread
class worker(Thread):
def run(self):
for x in xrange(0,111):
print x
time.sleep(1)
def run():
worker().start()
if __name__ == '__main__':
run()
每秒打印一次
我直接用python text.py 执行 没问题 每秒输出一个数
但我在后台执行:
Python代码
python test.py > log.log&
还是不行。开始不输出 直到程序执行完,一次性的写到log.log文件了。
为什么呢?
原因可能是python 的print 先写到缓冲区了,还没flush到文件。
于是加了一行“ sys.stdout.flush()” --在每次print后都flush一次。:
Java代码
import sys,time
from threading import Thread
class worker(Thread):
def run(self):
for x in xrange(0,111):
print x
sys.stdout.flush()
time.sleep(1)
def run():
worker().start()
if __name__ == '__main__':
run()
问题解决。
===============================================================================
还可以:python-u
xxx.py > log.log & ,再细看下帮助文档:man python
PYTHON(1) PYTHON(1)
NAME
python - an interpreted, interactive, object-oriented programming language
SYNOPSIS
python [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ] [ -O ]
[ -Q argument ] [ -S ] [ -t ] [ -u ]
[ -v ] [ -V ] [ -W argument ] [ -x ] [ -3 ]
[ -c command | script | - ] [ arguments ]
DESCRIPTION
Python is an interpreted, interactive, object-oriented programming language that combines remarkable power with very clear syntax.
For an introduction to programming in Python you are referred to the Python Tutorial. The Python Library Reference documents built-in
and standard types, constants, functions and modules. Finally, the Python Reference Manual describes the syntax and semantics of the
core language in (perhaps too) much detail. (These documents may be located via the INTERNET RESOURCES below; they may be installed
on your system as well.)
Python’s basic power can be extended with your own modules written in C or C++. On most systems such modules may be dynamically
loaded. Python is also adaptable as an extension language for existing applications. See the internal documentation for hints.
Documentation for installed Python modules and packages can be viewed by running the pydoc program.
COMMAND LINE OPTIONS
-c command
Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments
to the command).
-d Turn on parser debugging output (for wizards only, depending on compilation options).
-E Ignore environment variables like PYTHONPATH and PYTHONHOME that modify the behavior of the interpreter.
-h Prints the usage for the interpreter executable and exits.
-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the
command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a
script raises an exception.
-m module-name
Searches sys.path for the named module and runs the corresponding .py file as a script.
-u Force
stdin, stdout and stderr to be totallyunbuffered.
On systems where it matters, also put stdin, stdout and stderr in
binary mode. Note that there is internal buffering in xreadlines(), readlines() and file-object
iterators ("for line in
sys.stdin") which is not influenced by this option. To work around this, you will want to use "sys.stdin.readline()" inside a
"while 1:" loop.
-v Print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded.
When given twice, print a message for each file that is checked for when searching for a module. Also provides information on
module cleanup at exit.
-V Prints the Python version number of the executable and exits.
相关文章推荐
- 后台运行python程序 遇到缓冲区问题
- 后台运行python程序 遇到缓冲区问题
- 后台运行python程序 遇到缓冲区问题
- 后台运行python程序输出缓冲区问题
- python MySQLdb 问题 - 程序已的后台运行每90秒获取数据,却无法获取最新的值
- Python自学遇到的小问题——交互模式下程序从某句直接运行了
- 【python】安装python第三方库lxml时,遇到问题:[ERROR: 'xslt-config' 不是内部或外部命令,也不是可运行的程序]
- 如题,我用的是vs2008(测试版)和UltraEdit-32 version 12.10我一运行或者调试web 程序就会遇到这个问题
- C程序访问hadoop运行时遇到的各种问题以及解决方案(环境变量不被sudo继承)
- python 后台运行问题
- 运行VTK自带的Qt例子GraphicsView遇到的程序运行崩溃问题
- 在eclipse上运行MapReduce的wordcount程序所遇到的问题
- android基础(android程序的后台运行问题)
- IIS7运行.net3.5程序遇到一个语言问题
- Windows后台运行python程序
- 快速解决打印机后台程序服务没有运行的问题
- 装sql server sp4时,遇到的问题:以前进行的程序安装创建了挂起的文件操作.运行程序之前,必须重新起动计算机
- PYTHON的程序在LINUX后台运行
- PYTHON的程序在LINUX后台运行
- 用新版本的job打成jar包在终端运行mapreduce程序遇到的问题 -找不到Map class?