您的位置:首页 > 运维架构 > Shell

Shell执行python程序的缓冲区问题

2016-12-27 17:12 495 查看
环境:Linux

一段执行时间很长的Python程序在linux后台执行,把结果输出到某文件:

cmd='python  '$1' '$2' '$3' '$5' '$4
RESULT=eval $cmd
echo $RESULT


注:1,2,3,4,5为系统参数,指向Python文件及向Python传入的参数。

遇到的问题,程序没报错,echo却没有任何输出。

按照前一篇博客的方法可以解决这个问题,但是需要修改原有Python程序。现在提供一种新的解决方式:

修改shell如下:

cmd='python -u '$1' '$2' '$3' '$5' '$4
RESULT=eval $cmd
echo $RESULT


两者的区别是在python命令之后添加-u参数。

很多人对于python命令的参数不太清楚,其实可以使用man python命令查看:

PYTHON(1)                                                            PYTHON(1)

NAME
python - an interpreted, interactive, object-oriented programming language

SYNOPSIS
python [ -B ] [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ]
[ -O ] [ -O0 ] [ -R ] [ -Q argument ] [ -s ] [ -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鈙  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
-B     Don鈚 write .py[co] files on import. See also PYTHONDONTWRITEBYTECODE.

-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 ,  -? ,  --help
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 vari-
ables 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.

-O     Turn on basic optimizations.  This changes the filename extension for compiled (bytecode) files from .pyc to .pyo.  Given twice, causes docstrings to be discarded.

-O0    Discard docstrings in addition to the -O optimizations.

-R     Turn  on "hash randomization", so that the hash() values of str, unicode, buffer and datetime objects are "salted" with an unpredictable pseudo-random value. Although they remain constant within an individual Python pro-
cess, they are not predictable between repeated invocations of Python.

This is intended to provide protection against a denial of service caused by carefully-chosen inputs that exploit the worst case performance of a dict  construction,  O(n^2)  complexity.   See  http://www.ocert.org/advi- sories/ocert-2011-003.html for details.

-Q argument
Division  control; see PEP 238.  The argument must be one of "old" (the default, int/int and long/long return an int or long), "new" (new division semantics, i.e. int/int and long/long returns a float), "warn" (old divi-
sion semantics with a warning for int/int and long/long), or "warnall" (old division semantics with a warning for all use of the division operator).  For a use of "warnall", see the Tools/scripts/fixdiv.py script.

-s     Don鈚 add user site directory to sys.path.

-S     Disable the import of the module site and the site-dependent manipulations of sys.path that it entails.

-t     Issue a warning when a source file mixes tabs and spaces for indentation in a way that makes it depend on the worth of a tab expressed in spaces.  Issue an error when the option is given twice.

-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 mod-
ule.  Also provides information on module cleanup at exit.

-V ,  --version
Prints the Python version number of the executable and exits.

-W argument
Warning control.  Python sometimes prints warning message to sys.stderr.  A typical warning message has the following form: file:line: category: message.  By default, each warning is printed once  for  each  source  line
where  it  occurs.   This  option controls how often warnings are printed.  Multiple -W options may be given; when a warning matches more than one option, the action for the last matching option is performed.  Invalid -W
options are ignored (a warning message is printed about invalid options when the first warning is issued).  Warnings can also be controlled from within a Python program using the warnings module.

The simplest form of argument is one of the following action strings (or a unique abbreviation): ignore to ignore all warnings; default to explicitly request the default behavior (printing each warning  once  per  source
line);  all  to print a warning each time it occurs (this may generate many messages if a warning is triggered repeatedly for the same source line, such as inside a loop); module to print each warning only the first time
it occurs in each module; once to print each warning only the first time it occurs in the program; or error to raise an exception instead of printing a warning message.

The full form of argument is action:message:category:module:line.  Here, action is as explained above but only applies to messages that match the remaining fields.  Empty fields match all values;  trailing  empty  fields
may  be  omitted.   The message field matches the start of the warning message printed; this match is case-insensitive.  The category field matches the warning category.  This must be a class name; the match test whether
the actual warning category of the message is a subclass of the specified warning category.  The full class name must be given.  The module field matches the (fully-qualified) module name; this match  is  case-sensitive.
The line field matches the line number, where zero matches all line numbers and is thus equivalent to an omitted line number.

-x     Skip the first line of the source.  This is intended for a DOS specific hack only.  Warning: the line numbers in error messages will be off by one!

-3     Warn about Python 3.x incompatibilities that 2to3 cannot trivially fix.


**

-u的意思是: 强制stdin,stdout和stderr为无缓冲模式,而对于xreadlines(),readlines()和文件对象迭代器中有内部缓冲,不受此项影响。

**
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐