后台运行python程序 遇到缓冲区问题
From: http://www.iteye.com/topic/867446
环境:linux
一段执行时间很长的程序(用python做hive客户端执行mapreduce) 在linux后台执行,把结果输出到某文件:
- python xxx.py > log.log&
遇到的问题,程序没报错,文件里却什么都没有,没有输入进去。为什么呢?
于是我首先尝试用:
- nohup python xxx.py > log.log &
预料之中 还是不行。
于是我做实验:
写了个test.py:
- import sys,time
- from threading import Thread
- class worker(Thread):
- def run(self):
- ,111):
- print x
- )
- def run():
- worker().start()
- if __name__ == '__main__':
- run()
每秒打印一次
我直接用python text.py 执行 没问题 每秒输出一个数
但我在后台执行:
- python test.py > log.log&
还是不行。开始不输出 直到程序执行完,一次性的写到log.log文件了。
为什么呢?
原因可能是python 的print 先写到缓冲区了,还没flush到文件。
于是加了一行“ sys.stdout.flush()” --在每次print后都flush一次。:
- import sys,time
- from threading import Thread
- class worker(Thread):
- def run(self):
- ,111):
- print x
- sys.stdout.flush()
- )
- 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程序 遇到缓冲区问题的更多相关文章
- Linux(9)后台运行python程序并输出到日志文件
后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...
- 在windows中:双击运行Python程序、后台运行Python程序
在windows中:双击运行Python程序.后台运行Python程序 安装Python解释器的windows环境,如果双击运行*.py的文件,会闪退.怎样避免闪退呢? 我们用python的日志输出程 ...
- Linux后台运行python程序并输出到日志文件
后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...
- linux后台运行python程序 nohup
nohup python -u test.py > out.log 2>&1 & nohup sh **.sh > /dev/null 2>&1 &am ...
- centos后台运行python程序
在服务器上,为了退出终端,程序依然能够运行,需要设置程序在后台运行. 关键的命令:nohup *基本用法:进入要运行的py文件目录前 nohup python -u test.py > tes ...
- 运行python程序
1 在windows下运行python程序 1)从DOS命令行运行python脚本 用python解释器来执行python脚本,在windows下面python解释器是python.exe,我的pyt ...
- editplus3运行Python程序
editplus3是一款不错的编辑器,他可以编译,运行java,php等各种程序,现把他运行Python程序的方法贴出来,首先得安装python,然后打开editplug3,工具——配置用户工具——组 ...
- 如何使用sublime编辑器运行python程序
现在越发喜欢sublime编辑器了,不仅界面友好美观.文艺,可扩展性还特别强. sublime本身是不具备运行python程序的能力的,需要做些设置才可以.以下是安装好sublime后设置的步骤: 点 ...
- 周一02.3运行python程序的两种方式
一.运行python程序的两种方式 方法一:交互式: 优点:输入一行代码立刻返回结果 缺点:无法永久保存代码 方法二: ...
随机推荐
- react: next-redux-saga
instead of using the Provider component, you can use the withRedux higher order component to inject ...
- List列表按照对象进行排序
在某些业务场景下需要根据list列表中对象的字段进行排序.今天就以实例说明: 实体类 public class Product { private int discount; // 省略getter/ ...
- SVN 用户名切换
方法1:主要针对Window下安装了客户端设置 右键桌面-->settings-->Saved Data -->Authentication data -->clear 方法2 ...
- keras&tensorflow+分布式训练︱实现简易视频内容问答框架
内容来源:Keras 之父讲解 Keras:几行代码就能在分布式环境训练模型 把 Keras API 直接整合入 TensorFlow 项目中,这样能与你的已有工作流无缝结合.至此,Keras 成为了 ...
- jmeter 查看提取的参数
需求:查看“传入的参数”或者“正则表达提取的参数”等...... 解决:添加Debug Sampler组件,不需要配置,直接使用默认 1.使用CSV Data Set Config组件“传入的参数”直 ...
- EL表达式可以直接放在url的“ ”里面
<div class="hc-prm-search search flr"> <form action="/userCenter/projectInfo ...
- python学习-实现用户密码登录,输错三次锁定
作业需求: 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 实现思路: 判断用户是否在黑名单,若在黑名单,则将用户锁定 判断用户是否存在,若不存在,提示用户不存在 若用户存在,判断登录密码是否 ...
- 洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver
传送门 题目大意:n头牛在单行道n个位置,开始用不同的速度跑步. 当后面的牛追上前面的牛,后面的牛会和前面的牛以一样的速度 跑,称为一个小团体.问:ts后有多少个小团体. 题解:模拟 倒着扫一遍,因为 ...
- Ubantu 新建用户后没有生成对应文件夹
原命令:useradd python 改正后:useradd python -m 后成功在home目录下创建文件夹 原因: man useradd就可以看到如此介绍:Create the user´s ...
- STM32高级定时器用于普通定时,定时周期变长
最近在用stm32定时器控制步进电机,由于普通定时器不够用,只能把TIM1当普通定时器用,我随手就把普通定时器的代码搬过去. void cs_Timer_Init(void) //TIM1 us级 { ...