后台运行python程序 遇到缓冲区问题

From: http://www.iteye.com/topic/867446

环境:linux

 

一段执行时间很长的程序(用python做hive客户端执行mapreduce) 在linux后台执行,把结果输出到某文件:

 

 

Python代码  收藏代码
  1. python xxx.py > log.log&   

 

 遇到的问题,程序没报错,文件里却什么都没有,没有输入进去。为什么呢?

于是我首先尝试用:

 

 

Java代码  收藏代码
  1. nohup python xxx.py > log.log &  

 

预料之中 还是不行。

 

于是我做实验:

 

写了个test.py:

 

Java代码  收藏代码
  1. import sys,time  
  2. from threading import Thread  
  3. class worker(Thread):  
  4.      def run(self):  
  5.          for x in xrange(0,111):  
  6.              print x  
  7.              time.sleep(1)  
  8. def run():  
  9.      worker().start()  
  10. if __name__ == '__main__':  
  11.     run()  

 每秒打印一次

我直接用python text.py 执行  没问题  每秒输出一个数

但我在后台执行:

 

Python代码  收藏代码
  1. python test.py > log.log&   

 

还是不行。开始不输出  直到程序执行完,一次性的写到log.log文件了。

为什么呢? 

原因可能是python 的print 先写到缓冲区了,还没flush到文件。

于是加了一行“ sys.stdout.flush()” --在每次print后都flush一次。:

 

Java代码  收藏代码
  1. import sys,time  
  2. from threading import Thread  
  3. class worker(Thread):  
  4.      def run(self):  
  5.          for x in xrange(0,111):  
  6.              print x  
  7.              sys.stdout.flush()    
  8.              time.sleep(1)  
  9. def run():  
  10.      worker().start()  
  11. if __name__ == '__main__':  
  12.     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 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.




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/406567.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[nodejs][html5][css3][js] 个人网站上线

各个功能详细代码 http://www.cnblogs.com/wangxinsheng/p/4263591.html 2015年1月31日 --- 虽然比较懒,但终于匆忙的弄了个个人网站上线,没有博客功能。。。只有些数据抓取,百度地图,视屏游戏功能。 可是heroku站点在国内的速度超…

各种URL生成方式的性能对比

在上一篇文章中我们列举了各种URL生成的方式,其中大致可以分为三类: 直接拼接字符串(方法一及方法二) 使用Route规则生成URL(方法三) 使用Lambda表达式生成URL(方法四及方法五) 我们…

element-ui中el-table的表头、内容样式

方式1&#xff1a; 直接在标签上添加上属性值&#xff1a; <el-table:header-cell-style"{background:#F3F4F7,color:#555}" ></el-table>方式2&#xff1a; 在method里面写上方法&#xff1a; rowClass({ row, rowIndex}) {console.log(rowIndex) //表…

python下设置urllib连接超时

From: http://blog.csdn.net/vah101/article/details/6175406 首先导入socket库 import socket 在开始连接前的代码中&#xff0c;再加入 socket.setdefaulttimeout(6) #6秒内没有打开web页面&#xff0c;就算超时 然后就可以开始连接了&#xff0c;比如 try: …

请移步到我的新浪博客

请移步到我的新浪博客http://blog.sina.com.cn/highlandcat转载于:https://blog.51cto.com/highlandcata/221449

疯狂喷气机

2/3D游戏&#xff1a;2D 辅助插件&#xff1a;原生 游戏制作难度系数&#xff1a;初级 游戏教程网址&#xff1a;http://www.raywenderlich.com/69392/make-game-like-jetpack-joyride-unity-2d-part-1 1、控制摄像机跟随人物移动 public GameObject targetObject; //目标对象p…

elementui表格-改变某一列的样式

cellStyle({ row, column, rowIndex, columnIndex }) {if (columnIndex 0) {// 指定列号return ‘padding:0‘} else {return ‘‘} },

正则表达式基础(一)

From: http://www.usidcbbs.com/read-htm-tid-1457.html Perl 中的正则表达式 正则表达式是 Perl 语言的一大特色&#xff0c;也是 Perl 程序中的一点难点&#xff0c;不过如果大家能够很好的掌握他&#xff0c;就可以轻易地用正则表达式来完成字符串处理的任务&#xff0…

CodeSmith--SchemaExplorer类结构详细介绍

CodeSmith----SchemaExplorer类结构详细介绍 CodeSmith与数据库的联系&#xff0c;在CodeSmith中自带一个程序集SchemaExplorer.dll&#xff0c;这个程序集中的类主要用于获取数据库中各种对象的结构。 <% Property Name"SourceTable" Type"SchemaExplorer.T…

vue element项目常见实现表格内部可编辑功能

目录 前言 正文 1.简单表格行内内部可编辑 2. 数据从后端取得表格行内可编辑 3.批量表格整体的可编辑 结语 前言 后台系统都是各种表格表单编辑&#xff0c;整理了下常见的几种实现表格编辑的方式&#xff0c;希望有用。使用框架&#xff1a;vueelement 表格行内内部可编辑 数…

tar

tar命令是Unix的一个shell命令&#xff0c;该命令可在为多个制定文件创建一个档案文件&#xff0c;也可以从一个档案文件中解压缩出文件。tar档案文件的扩展名为“.tar”。tar包中的文件并不是压缩文件&#xff0c;而是所有文件集合成的一个文件。 tar这个名字源自在磁带上备份…

Yii2.0 技巧总结

View部分 1. 使用ActiveField中的hint生成提示文字 <? $form->field($model, freightAddedFee)->textInput()->hint(大于0的整数) ?> 2. 文本框添加placeholder属性&#xff0c;其实这个本来就是html5带的属性。 <? $form->field($model, mobile, $inp…

React开发(157):一级直接用getFieldDecorator

<Row gutter{12}><Col span{12}><Form.Item label"省/市/区">{getFieldDecorator(proviceValue, {initialValue: proviceValue,rules: [{ required: true, message: 公司人数不能为空 }],})(<CascaderfieldNames{fieldNames}options{options}on…

【JavaScript】appendChild一个的注意点之会删除原dom树节点

最近在研究学习vue原理&#xff0c;其中使用createDocumentFragment()方法&#xff0c;是用来创建一个虚拟的节点对象&#xff0c;那问题来了&#xff0c;创建了虚拟dom树&#xff0c;且最后只渲染了虚拟dom树里面的节点&#xff0c;那原dom树的节点去哪里了&#xff0c;查阅了…

正则表达式图书

From: http://www.usidcbbs.com/read-htm-tid-1457-page-2.html 网文 vs 书藉 只要是知道“正则”这个词的&#xff0c;上网搜集个把资料&#xff0c;应该就不是问题吧。我获得正则消息的网絡渠道有这样几个&#xff0c;以质量从高到低排序&#xff1a;dilicious标签&#xff0…

Spring.NET学习笔记12——面向切面编程(基础篇) Level 300

AOP即面向切面编程(Aspect Oriented Programming的缩写)&#xff0c;是OOP(面向对象编程)的一种延续形式。是通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术&#xff0c;它从一个不同于OOP的角度来看待程序的结构&#xff1a;OOP将…

React开发(158):ant design级联回显 直接传入数组

ReactDOM.render(<CascaderdefaultValue{[zhejiang, hangzhou, xihu]}options{options}onChange{onChange}/>,mountNode, );