后台运行python程序 遇到缓冲区问题
From:?http://www.iteye.com/topic/867446
環(huán)境:linux
?
一段執(zhí)行時間很長的程序(用python做hive客戶端執(zhí)行mapreduce) 在linux后臺執(zhí)行,把結(jié)果輸出到某文件:
?
?
Python代碼???
?遇到的問題,程序沒報錯,文件里卻什么都沒有,沒有輸入進去。為什么呢?
于是我首先嘗試用:
?
?
Java代碼???
預(yù)料之中 還是不行。
?
于是我做實驗:
?
寫了個test.py:
?
Java代碼???每秒打印一次
我直接用python text.py 執(zhí)行 ?沒問題 ?每秒輸出一個數(shù)
但我在后臺執(zhí)行:
?
Python代碼???
還是不行。開始不輸出 ?直到程序執(zhí)行完,一次性的寫到log.log文件了。
為什么呢??
原因可能是python 的print 先寫到緩沖區(qū)了,還沒flush到文件。
于是加了一行“ sys.stdout.flush()” --在每次print后都flush一次。:
?
Java代碼??問題解決。
===============================================================================
還可以: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 totally unbuffered. ?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.
總結(jié)
以上是生活随笔為你收集整理的后台运行python程序 遇到缓冲区问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 和硕看重物联网大势 程建中:从擅长领域出
- 下一篇: 一次学生时代的经历,利用Python在机