Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python子进程(Popen)中丢失一些std输出读数_Python_Python 2.7_Subprocess_Stdout - Fatal编程技术网

从Python子进程(Popen)中丢失一些std输出读数

从Python子进程(Popen)中丢失一些std输出读数,python,python-2.7,subprocess,stdout,Python,Python 2.7,Subprocess,Stdout,我有一个用Python编写的程序,在某个点上创建一个子进程,然后必须通过一个文件实时获取其std输出。该进程需要一段时间,并且在它仍在运行时需要一些输出。 问题是,有时候,输出的一个块丢失了,实际上是从一开始就丢失的一个块。代码如下所示: 导入子流程 导入临时文件 导入时间 .. tf_out=tempfile.TemporaryFile tf_err=tempfile.TemporaryFile tf_in=tempfile.TemporaryFile tf_in.writesome_stdi

我有一个用Python编写的程序,在某个点上创建一个子进程,然后必须通过一个文件实时获取其std输出。该进程需要一段时间,并且在它仍在运行时需要一些输出。 问题是,有时候,输出的一个块丢失了,实际上是从一开始就丢失的一个块。代码如下所示:

导入子流程 导入临时文件 导入时间 .. tf_out=tempfile.TemporaryFile tf_err=tempfile.TemporaryFile tf_in=tempfile.TemporaryFile tf_in.writesome_stdin tf_in.seek0 创建流程 process=subprocess.Popencommand,shell=True, stdin=tf_in, stdout=tf_out, stderr=tf_err 运行=真 运行时: 若进程结束,则为最后一次迭代 如果process.poll不是None: 运行=错误 读取输出 tf_out.seek0 1 stdout=tf_out.read 2 对部分输出做些什么 做点什么 时间。睡眠0.1 关闭 tf_错误关闭 关闭 .. 我想知道问题是否可能在1和2之间,如果子进程写了一些东西,然后在1处寻找到零,由于寻找错误,另一次写入从零开始,最后点2处的读取没有看到第一次写入。如果这是问题所在,你知道怎么做吗


提前谢谢

从Popen获取实时数据相当挑剔。此外,好的旧打印通常也会缓冲输出

以下是根据另一个SO问题改编的。它已经设置好了,所以你可以直接运行它

来源 输出 左侧的数字是自命令启动后的秒数。由于ping命令每秒发送一次数据包,这将验证数据是否得到实时处理。当ping开始时,两条0.05线被吐出。当接收到第一个ping响应时,下一行在第二行之后。在时间2.05收到最后一个响应,ping输出一个页脚


非常方便

我刚刚想出了解决办法。一个简单的解决方案是只打开临时文件两次,一次传递给子流程,另一次实际执行读取。此外,您可以使用tempfile模块创建命名的临时文件,如下所示:

import tempfile

tf_in = tempfile.TemporaryFile()
tf_in.write(some_stdin)
tf_in.seek(0)

tf_out = tempfile.NamedTemporaryFile()
tf_out2 = open(tf_out.name, 'r')
tf_err = tempfile.NamedTemporaryFile()
tf_err2 = open(tf_err.name, 'r')

# create the process
process = subprocess.Popen(command, shell=True,
            stdin=tf_in,
            stdout=tf_out,
            stderr=tf_err)

# same loop but reading from tf_out2 and tf_err2

# close files
使用两个文件对象进行写入和读取可以解决这个问题


无论如何,谢谢你的回答

是否有一个原因您不能使用,这将允许您直接从process.stdout/process.stdin读取/写入?Dano,据我所知,这些对象是在进程结束时写入的,因此我无法使用它们实时获取输出。实际上,我已经用一种非常简单的方法解决了这一问题。无论如何,非常感谢你的回答!
COMMAND: ping -c3 8.8.8.8
0.05  PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
0.05  64 bytes from 8.8.8.8: icmp_seq=1 ttl=43 time=49.0 ms
1.05  64 bytes from 8.8.8.8: icmp_seq=2 ttl=43 time=46.8 ms
2.05  64 bytes from 8.8.8.8: icmp_seq=3 ttl=43 time=49.1 ms
2.05  
2.05  --- 8.8.8.8 ping statistics ---
2.05  3 packets transmitted, 3 received, 0% packet loss, time 2003ms
2.05  rtt min/avg/max/mdev = 46.826/48.334/49.143/1.097 ms
import tempfile

tf_in = tempfile.TemporaryFile()
tf_in.write(some_stdin)
tf_in.seek(0)

tf_out = tempfile.NamedTemporaryFile()
tf_out2 = open(tf_out.name, 'r')
tf_err = tempfile.NamedTemporaryFile()
tf_err2 = open(tf_err.name, 'r')

# create the process
process = subprocess.Popen(command, shell=True,
            stdin=tf_in,
            stdout=tf_out,
            stderr=tf_err)

# same loop but reading from tf_out2 and tf_err2

# close files