Python不断阅读Popen(Windows) [英] Python reading Popen continuously (Windows)

查看:567
本文介绍了Python不断阅读Popen(Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试 stdout.readline 并在多处理过程中将结果(即每行打印到终端时)放入.Queue 我们在另一个.py文件中。但是,调用:

Im trying to stdout.readline and put the results (i.e each line, at the time of printing them to the terminal) on a multiprocessing.Queue for us in another .py file. However, the call:

res = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=1 )
with res.stdout:
    for line in iter(res.stdout.readline, b''):
        print line
res.wait()

在过程完成后将阻止并且将打印结果(如果退出代码不是,则根本不打印) t返回)。

Will block and the results will be printed after the process is complete (or not at all if exit code isn't returned).

我已经浏览了SO以获得答案,并尝试设置bufsize = 1,产生处理读数的线程,使用filedescriptor等。似乎无效。我可能不得不使用模块 pexpect 但我不确定它是如何工作的。

I've browsed SO for answers to this, and tried setting bufsize=1, spawning threads that handle the reading, using filedescriptors, etc. None seem to work. I might have to use the module pexpect but I'm not sure how it works yet.

我也试过

 def enqueue_output(self, out, queue):
    for line in iter(out.readline, b''):
        queue.put([line])
    out.close()

将数据放入队列,但由于 out.readline 似乎阻止了,结果将是相同的。

To put the data on the queue, but since out.readline seems to block, the result will be the same.

简而言之:如何在打印时将子进程输出提供给我?它一次打印1-10行的块,但是当这个过程完成时会返回给我,同时也用换行符分隔。

In short: How do I make the subprocess output available to me at the time of print? It prints chunks of 1-10 lines at a time, however these are returned to me when the process completes, separated by newlines as well..

相关:

Python子流程readlines()挂起

Python :从subprocess.communicate()读取流媒体输入

在python上的subprocess.PIPE上的非阻塞读取

推荐答案

正如@eryksun所解释的那样,并且你的评论已经确认,缓冲的原因是C应用程序使用 printf

As explained by @eryksun, and confirmed by your comment, the cause of the buffering is the use of printf by the C application.

默认情况下,printf缓冲其输出,但输出在换行时刷新或者如果发生读取当输出定向到终端时。当输出定向到文件或管道时,实际输出仅在缓冲区已满时发生。

By default, printf buffers its output, but the output is flushed on newline or if a read occurs when the output is directed to a terminal. When the output is directed to a file or a pipe, the actual output only occurs when the buffer is full.

幸运的是,在Windows上,没有低级缓冲(* )。这意味着在程序开头附近调用 setvbuf(stdout,NULL,_IONBF,0); 就足够了。但不幸的是,你根本不需要缓冲( _IONBF ),因为Windows上的行缓冲实现为完全缓冲。

Fortunately on Windows, there is no low level buffering (*). That means that calling setvbuf(stdout, NULL, _IONBF, 0); near the beginning of the program would be enough. But unfortunately, you need no buffering at all (_IONBF), because line buffering on Windows is implemented as full buffering.

(*)在Unix或Linux系统上,底层系统调用可以添加自己的缓冲。这意味着使用低级别 write(1,buf,strlen(buf)); 的程序将在Windows上无缓冲,但在标准输出时仍将在Linux上进行缓冲连接到管道或文件。

(*) On Unix or Linux systems, the underlying system call can add its own buffering. That means that a program using low level write(1, buf, strlen(buf)); will be unbuffered on Windows, but will still be buffered on Linux when standard output is connected to a pipe or a file.

这篇关于Python不断阅读Popen(Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆