我试图stdout
通过subprocess.Popen
电话抓住,虽然我通过这样做很容易实现:
cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE)
for line in cmd.stdout.readlines():
print line
Run Code Online (Sandbox Code Playgroud)
我想stdout
"实时" 抓住.使用上面的方法,PIPE正在等待抓取所有stdout
,然后它返回.
因此,对于记录目的,这不符合我的要求(例如,"看到"发生时发生的事情).
有没有办法一行一行地stdout
运行?或者这是一个限制subprocess
(必须等到PIPE
关闭).
编辑
如果我转readlines()
了readline()
,我只得到了最后一行stdout
(不理想):
In [75]: cmd = Popen('ls -l', shell=True, stdout=PIPE)
In [76]: for i in cmd.stdout.readline(): print i
....:
t
o
t
a
l
1
0
4
Run Code Online (Sandbox Code Playgroud) 是否有任何参数或选项为Python的subprocess.Popen方法设置超时?
像这样的东西:
subprocess.Popen(['..'], ..., timeout=20)
?
我使用subprocess.popen()函数在python中执行命令,如下所示:
omp_cmd = 'cat %s | omp -h %s -u %s -w %s -p %s -X -' %(temp_xml, self.host_IP, self.username, self.password, self.port)
xmlResult = Popen(omp_cmd, stdout=PIPE, stderr=STDOUT)
Run Code Online (Sandbox Code Playgroud)
在shell中它运行正常没有错误,但在python我得到:
File "/home/project/vrm/apps/audit/models.py", line 148, in sendOMP
xmlResult = Popen(omp_cmd, stdout=PIPE, stderr=STDOUT)
File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
我搜索了错误,但没有一个解决了我的问题.有谁知道这个问题的原因是什么?谢谢.
我有一个带有GUI的程序,通过Popen调用运行外部程序:
p = subprocess.Popen("<commands>" , stdout=subprocess.PIPE , stderr=subprocess.PIPE , cwd=os.getcwd())
p.communicate()
Run Code Online (Sandbox Code Playgroud)
但是无论我做什么,都会弹出控制台(我也尝试将NUL传递给文件句柄).如果没有得到我调用的二进制文件来释放它的控制台,有没有办法做到这一点?
我正在尝试制作一个有按钮的PyGtk Gui.当用户按下此按钮时,gnome-terminal
提示用户输入密码.
然后它将为JQuery片段克隆这个Git存储库gedit
.
然后,它将js.xml
文件复制到/usr/share/gedit/plugins/snippets/js.xml
最后,它强制删除了Git存储库.
命令:
gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery
Run Code Online (Sandbox Code Playgroud)
它在我的终端工作正常.
但是,通过它刚打开的GUI,我添加了我的密码,按回车键,然后再次关闭.
我只想将命令运行到第一个命令 &&
这是我的Python函数(带命令):
def on_install_jquery_code_snippet_for_gedit_activate(self, widget):
""" Install Jquery code snippet for Gedit. """
cmd="gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
close_fds=False)
self.status.set_text(p.stdout.read()) #show response in 'status
Run Code Online (Sandbox Code Playgroud) 我有以下情况:我在socketio服务器上收到请求.我回答它(socket.emit(..))然后在另一个线程中启动一些计算负载很重的东西.
如果繁重的计算是由subprocess.Popen
(使用subprocess.PIPE
)引起的,它会完全阻止每个传入的请求,只要它正在被执行,尽管它发生在一个单独的线程中.
没问题 - 在这个线程中,建议异步读取子进程的结果,缓冲区大小为1,以便在这些读取之间,其他线程有机会做某事.不幸的是,这对我没有帮助.
我也已经monkeypatched eventlet和工作正常-只要我不使用subprocess.Popen
与subprocess.PIPE
在线程.
在此代码示例中,您可以看到它只在使用subprocess.Popen
时发生subprocess.PIPE
.当取消注释#functionWithSimulatedHeavyLoad()
并反而评论functionWithHeavyLoad()
一切都像魅力一样.
from flask import Flask
from flask.ext.socketio import SocketIO, emit
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)
socketio = SocketIO(app)
import time
from threading import Thread
@socketio.on('client command')
def response(data, type = None, nonce = None):
socketio.emit('client response', ['foo'])
thread = Thread(target = testThreadFunction)
thread.daemon = True
thread.start()
def testThreadFunction(): …
Run Code Online (Sandbox Code Playgroud) 让我们考虑一下这个片段:
from subprocess import Popen, PIPE, CalledProcessError
def execute(cmd):
with Popen(cmd, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)
base_cmd = [
"cmd", "/c", "d:\\virtual_envs\\py362_32\\Scripts\\activate",
"&&"
]
cmd1 = " ".join(base_cmd + ['python -c "import sys; print(sys.version)"'])
cmd2 = " ".join(base_cmd + ["python -m http.server"])
Run Code Online (Sandbox Code Playgroud)
如果我运行execute(cmd1)
输出将打印没有任何问题.
但是,如果我运行execute(cmd2)
而不会打印任何内容,为什么会这样,我如何修复它,以便我可以实时看到http.server的输出.
另外,如何for line in p.stdout
在内部进行评估?是什么样的无限循环,直到达到stoout eof或什么?
这个主题在SO中已经解决了几次,但我还没有找到一个Windows解决方案.上面的片段实际上是来自这个答案的代码并尝试从virtualenv运行http.server(在win7上运行python3.6.2-32bits)
我正在编写一个c ++程序,它执行并输出(实时)shell脚本,makefile或其他程序.但是,当有错误或没有错误时,我希望我的程序返回不同.
#include "execxi.h"
using namespace std;
int execXI::run(string command)
{
FILE *in;
char buff[512];
// is this the check for command execution exited with not 0?
if(!(in = popen(command.c_str(), "r"))){
// I want to return the exit code and error message too if any
return 1;
}
// this part echoes the output of the command that's executed
while(fgets(buff, sizeof(buff), in)!=NULL){
cout << buff;
}
pclose(in);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是我到目前为止.
假设这个脚本运行make
来构建一个程序,它给出了一个错误
on_target_webkit_version out/Release/obj/gen/webkit_version.h
Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud)