我试图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运行BASH内置命令?
我试过了:
subprocess.Popen(['bash','history'],shell=True, stdout=PIPE)
subprocess.Popen('history', shell=True, executable = "/bin/bash", stdout=subprocess.PIPE)
os.system('history')
Run Code Online (Sandbox Code Playgroud)
及其许多变化.我想跑history或fc -ln.
如何从输出中停止subprocess.Popen的输出?如果有很多印刷,印刷有时会很慢.
在python中,对于玩具示例:
for x in range(0, 3):
# call function A(x)
Run Code Online (Sandbox Code Playgroud)
我希望继续for循环,如果函数A跳过它需要超过5秒,这样我就不会卡住或浪费时间.
通过做一些搜索,我意识到子进程或线程可能会有所帮助,但我不知道如何在这里实现.任何帮助都会很棒.谢谢
我试图了解 python 3 的低级实现。subprocess模块使用了一个名为_posixsubprocess的模块。我试图在我的系统中找到这个模块的位置,发现它是一个存根文件。
有人可以指导我,因为我不知道什么是存根文件以及它们如何在较低级别实现?
我有一个程序产生并与CPU重,不稳定的进程通信,而不是由我创建.如果我的应用程序崩溃或被杀死SIGKILL,我希望子进程也被杀死,因此用户不必跟踪它们并手动终止它们.
我知道之前已经讨论过这个主题,但是我已经尝试了所描述的所有方法,并且它们似乎都没有在测试中幸存下来.
我知道它一定是可能的,因为终端一直都在做.如果我在终端中运行某些东西并杀死终端,这些东西总会死掉.
我试过atexit,双叉和ptys.atexit不起作用sigkill; 双叉根本不起作用; 和ptys我没有发现任何方式使用Python来工作.
今天,我发现了这一点prctl(PR_SET_PDEATHSIG, SIGKILL),这应该是儿童进程在父母去世时命令自杀的一种方式.我尝试使用它popen,但它接缝完全没有效果:
import ctypes, subprocess
libc = ctypes.CDLL('/lib/libc.so.6')
PR_SET_PDEATHSIG = 1; TERM = 15
implant_bomb = lambda: libc.prctl(PR_SET_PDEATHSIG, TERM)
subprocess.Popen(['gnuchess'], preexec_fn=implant_bomb)
Run Code Online (Sandbox Code Playgroud)
在上面,创建了子节点并且父节点退出.现在你会期望gnuchess得到一个SIGKILL和死,但它没有.我仍然可以在我的流程管理器中使用100%CPU找到它.
任何人都可以告诉我,我的使用是否有问题prctl?或者您是否知道终端如何设法杀死他们的孩子?
我正在使用启动各种子流程的第三方库.当有异常时我想杀死所有子进程.我如何获得儿童pids列表?
我希望能够通过脚本的返回值来定义变量.这就是我目前拥有的:
sum_total_earnings_usd = subprocess.call([SCRIPT, "-d", date])
Run Code Online (Sandbox Code Playgroud)
我已经检查了SCRIPT的返回值,但是,当我尝试设置此变量时,它总是返回0(http://docs.python.org/library/subprocess.html#subprocess.call).我如何运行此脚本并捕获返回值以存储为变量?
好的,我试图从python脚本运行一个C程序.目前我正在使用测试C程序:
#include <stdio.h>
int main() {
while (1) {
printf("2000\n");
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
模拟我将要使用的程序,它不断地从传感器获取读数.然后我试图"2000"从C程序读取输出(在这种情况下)与python中的子进程:
#!usr/bin/python
import subprocess
process = subprocess.Popen("./main", stdout=subprocess.PIPE)
while True:
for line in iter(process.stdout.readline, ''):
print line,
Run Code Online (Sandbox Code Playgroud)
但这不起作用.从使用print语句开始运行该.Popen行然后等待for line in iter(process.stdout.readline, ''):,直到我按下Ctrl-C.
为什么是这样?这正是我见过的大多数示例都是他们的代码,但它没有读取文件.
编辑:
有没有办法让它只在有东西被阅读时才能运行?
我在使用子进程模块获取崩溃程序的输出时遇到问题.我正在使用python2.7和subprocess来调用带有奇怪参数的程序以获得一些段错误为了调用程序,我使用以下代码:
proc = (subprocess.Popen(called,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE))
out,err=proc.communicate()
print out,err
Run Code Online (Sandbox Code Playgroud)
called是一个包含程序名称和参数的列表(一个包含随机字节的字符串,除了子进程根本不喜欢的NULL字节)
当程序没有崩溃时代码表现并向我显示stdout和stderr,但是当它崩溃时,out和err是空的而不是显示着名的"Segmentation fault".
即使程序崩溃,我希望找到一种方法来获取和错误.
希望有人在这里作为一个想法:)
PS:我也尝试过check_output/call/check_call方法
编辑:
我在一个python虚拟环境中的Archlinux 64位上运行这个脚本(这里不应该是重要的东西,但你永远不会知道:p)
segfault发生在我正在尝试运行的C程序中,是缓冲区溢出的结果
问题是当发生段错误时,我无法获得子进程发生的输出
我得到了正确的返回码:-11(SIGSEGV)
使用python我得到:
./dumb2 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
('Exit code was:', -11)
('Output was:', '')
('Errors were:', '')
Run Code Online (Sandbox Code Playgroud)在python外面,我得到:
./dumb2 $(perl -e "print 'A'x50")
BEGINNING OF PROGRAM
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
END OF THE PROGRAM
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)shell的返回值是相同的:echo $?返回139所以-11($?&128)
python ×10
subprocess ×10
bash ×1
c ×1
command ×1
communicate ×1
fuzzing ×1
hang ×1
logging ×1
pipe ×1
popen ×1
python-3.x ×1
sigkill ×1
system ×1