Python、子流程、管道和选择
我有一个python程序,其中我不断读取通过subprocess.Popen启动并通过subprocess.PIPE连接的其他程序的输出 我面临的问题是,它有时会失去启动程序的大部分输出 例如,通过管道对inotify事件进行监视,以Python、子流程、管道和选择,python,inotifywait,Python,Inotifywait,我有一个python程序,其中我不断读取通过subprocess.Popen启动并通过subprocess.PIPE连接的其他程序的输出 我面临的问题是,它有时会失去启动程序的大部分输出 例如,通过管道对inotify事件进行监视,以inotifywait丢失许多事件 这是相关的功能: process = subprocess.Popen(["inotifywait", "-q", "-r", "-m", "--format", "%e:::::%w%f", srcroot], s
inotifywait
丢失许多事件
这是相关的功能:
process = subprocess.Popen(["inotifywait", "-q", "-r", "-m",
"--format", "%e:::::%w%f", srcroot], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
polling = select.poll()
polling.register(process.stdout)
process.stdout.flush()
while True:
process.stdout.flush()
if polling.poll(max_seconds*1000):
line = process.stdout.readline()
if len(line) > 0:
print line[:-1]
process=subprocess.Popen([“inotifywait”、“-q”、“-r”、“-m”,
“--format”,%e:w%f,srcroot],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
polling=select.poll()
轮询.寄存器(进程.标准输出)
process.stdout.flush()
尽管如此:
process.stdout.flush()
如果轮询。轮询(最大秒数*1000):
line=process.stdout.readline()
如果len(线)>0:
打印行[:-1]
执行命令
inotifywait-q-r-m--format%e::%w%f/opt/fileserver/>/tmp/log1
并移动一些文件(以生成inotify事件),得到一个>8000行的文件。另一方面,使用my/pscript.py>/tmp/log2
给出一个大约5000行的文件。在您的示例中,您完全忽略了stderr。尝试创建如下过程:
process = subprocess.Popen(["inotifywait", "-q", "-r", "-m",
"--format", "%e:::::%w%f", srcroot], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
此外,我会将inotify直接用于它的一个Python,而不是用inotifywait生成一个进程。也尝试从stderr获取行,并打印该行,检查丢失的数据是否确实存在。-
print process.stderr.read()
不幸的是,上面的示例有些简化,因为我已经在检查stderr了。无论如何,谢谢你。不幸的是,上面的例子有些简化,因为我已经在检查stderr了。此外,我不能使用pyinotify,因为它的性能很慢(我尝试过,它可以处理数千个文件,但在我的情况下,它甚至不能创建足够数量的手表)。。。不管怎样,谢谢你。你不必设置成千上万的手表。您为要监视的每个文件夹设置了一个监视,应用程序将调用您的回调。如果您还需要监视子文件夹,则只需在创建监视时指定它。在高级,是的。但在低级别,pyinotify需要为任何不同的文件/目录建立监视。此过程非常耗时,并且存在严重的可伸缩性问题。纯C实现要快得多,主要是因为所有代码都是编译的。