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

环境:linux

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

  1. python xxx.py > log.log&

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

于是我首先尝试用:

  1. nohup python xxx.py > log.log &

预料之中 还是不行。

于是我做实验:

写了个test.py:

  1. import sys,time
  2. from threading import Thread
  3. class worker(Thread):
  4. def run(self):
  5. ,111):
  6. print x
  7. )
  8. def run():
  9. worker().start()
  10. if __name__ == '__main__':
  11. run()

每秒打印一次

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

但我在后台执行:

  1. python test.py > log.log&

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

为什么呢?

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

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

  1. import sys,time
  2. from threading import Thread
  3. class worker(Thread):
  4. def run(self):
  5. ,111):
  6. print x
  7. sys.stdout.flush()
  8. )
  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 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程序 遇到缓冲区问题的更多相关文章

  1. Linux(9)后台运行python程序并输出到日志文件

    后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...

  2. 在windows中:双击运行Python程序、后台运行Python程序

    在windows中:双击运行Python程序.后台运行Python程序 安装Python解释器的windows环境,如果双击运行*.py的文件,会闪退.怎样避免闪退呢? 我们用python的日志输出程 ...

  3. Linux后台运行python程序并输出到日志文件

    后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...

  4. linux后台运行python程序 nohup

    nohup python -u test.py > out.log 2>&1 & nohup sh **.sh > /dev/null 2>&1 &am ...

  5. centos后台运行python程序

    在服务器上,为了退出终端,程序依然能够运行,需要设置程序在后台运行. 关键的命令:nohup *基本用法:进入要运行的py文件目录前 nohup python  -u test.py > tes ...

  6. 运行python程序

    1 在windows下运行python程序 1)从DOS命令行运行python脚本 用python解释器来执行python脚本,在windows下面python解释器是python.exe,我的pyt ...

  7. editplus3运行Python程序

    editplus3是一款不错的编辑器,他可以编译,运行java,php等各种程序,现把他运行Python程序的方法贴出来,首先得安装python,然后打开editplug3,工具——配置用户工具——组 ...

  8. 如何使用sublime编辑器运行python程序

    现在越发喜欢sublime编辑器了,不仅界面友好美观.文艺,可扩展性还特别强. sublime本身是不具备运行python程序的能力的,需要做些设置才可以.以下是安装好sublime后设置的步骤: 点 ...

  9. 周一02.3运行python程序的两种方式

    一.运行python程序的两种方式 方法一:交互式:                     优点:输入一行代码立刻返回结果                      缺点:无法永久保存代码 方法二: ...

随机推荐

  1. input预览上传图片

    html代码 <input type="file" name="file" id="file" > <img src=&q ...

  2. Android面试题整理

    1.    请描述下Activity的生命周期. 2.    如果后台的Activity由于某原因被系统回收了,如何在被系统回收之前保存当前状态? 3.    如何将一个Activity设置成窗口的样 ...

  3. Golang 编译成 DLL 文件

    golang 编译 dll 过程中需要用到 gcc,所以先安装 MinGW. windows 64 位系统应下载 MinGW 的 64 位版本: https://sourceforge.net/pro ...

  4. hibernate的list和iterate的区别

    一.先介绍一下java中的缓存系统JCS(java cache system)  1.JCS(Java Caching System)是一个对象Cache,它可以把Java对象缓存起来,提高那些访问频 ...

  5. inline 关键字

    inline 定义一个函数时最好前面再加上static: static inline 定义的函数,会在链接阶段将代码段中没有使用的inline 函数定义“剔除”,从而减小编译体积:即使加了-g参数仍然 ...

  6. 完整的CRUD——javaweb

    1,总体架构 index是进去的页面, 可以跳转Insert的增加页面,operatePerson是根据传进来的URI来判断增删改查的页面, DbManager.java是封装的数据库操作类, Pag ...

  7. 开发沉思录 - 记大数据平台的一次 BUG井喷

    研究REST提交重启:终于提交成功了,首先一个错误:地址IP地址的http://hdp0:8080/api/v1/clusters/HDP/requests ,被我错误写成了request,当误了大量 ...

  8. Oracle中的存储过程简单例子

    --创建表create table TESTTABLE(  id1  VARCHAR2(12),  name VARCHAR2(32))select t.id1,t.name from TESTTAB ...

  9. ubuntu下使用code::blocks编译运行一个简单的gtk+2.0项目

    在具体的操作之前,首先需要安装一些必要的软件.ubuntu下默认安装了gcc,不过缺少必要的Header file,可以在命令行中输入下面的指令安装build-essential套件:sudo apt ...

  10. tomcat部署war包启动后请求无响应,一直报404

    刚刚碰到一个坑,本地Eclipse启动了tomcat,调用接口就是404,一直怀疑是url问题,改来改去还是404.最后发现还真是url问题,问题不在接口路径,而是项目名.因为接口路径用的是RESTf ...