- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经阅读了许多涉及实时标准输出打印的问题,包括 J.F. Sebastian's Python 3 solution 的回答阅读标准输出。
然而,虽然他的解决方案适用于这种情况:
with Popen(['ping'] + ['169.254.79.191'] + ['-c'] + ['5'], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
它无法像我预期的那样在我实际想要使用的应用程序中运行:
with Popen(['iperf3', '-c', '169.254.79.191', 'b', '100000000', '-p', '5202', 't', '5', '-R', '-V', '-u'], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
对于 ping 场景,每一行都被打印出来,就像我手动运行它一样。使用 iperf,它会在两行输出后停止,并在应用程序完成时刷新所有内容。如果我在脚本中依次执行它们,我会得到以下输出:
pi@raspberrypi2:~/project $ python3.4 stdout_RT_test.py
PING 169.254.79.191 (169.254.79.191) 56(84) bytes of data.
64 bytes from 169.254.79.191: icmp_seq=1 ttl=64 time=0.854 ms
64 bytes from 169.254.79.191: icmp_seq=2 ttl=64 time=0.867 ms
64 bytes from 169.254.79.191: icmp_seq=3 ttl=64 time=0.877 ms
64 bytes from 169.254.79.191: icmp_seq=4 ttl=64 time=0.842 ms
64 bytes from 169.254.79.191: icmp_seq=5 ttl=64 time=0.834 ms
--- 169.254.79.191 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3998ms
rtt min/avg/max/mdev = 0.834/0.854/0.877/0.040 ms
iperf 3.0.7
Linux raspberrypi2 4.4.50-v7+ #970 SMP Mon Feb 20 19:18:29 GMT 2017 armv7l GNU/Linux
IPERF 完成后显示此行之后的所有内容
Time: Wed, 29 Mar 2017 14:46:48 GMT
Connecting to host 169.254.79.191, port 5202
Reverse mode, remote host 169.254.79.191 is sending
Cookie: raspberrypi2.1490798808.947399.0490a
[ 4] local 169.254.181.167 port 41415 connected to 169.254.79.191 port 5202
Starting Test: protocol: UDP, 1 streams, 8192 byte blocks, omitting 0 seconds, 10 second test
[ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
[ 4] 0.00-1.00 sec 136 KBytes 1.11 Mbits/sec 1.907 ms 0/17 (0%)
[ 4] 1.00-2.00 sec 128 KBytes 1.05 Mbits/sec 0.966 ms 0/16 (0%)
[ 4] 2.00-3.00 sec 128 KBytes 1.05 Mbits/sec 0.634 ms 0/16 (0%)
[ 4] 3.00-4.00 sec 128 KBytes 1.05 Mbits/sec 0.522 ms 0/16 (0%)
[ 4] 4.00-5.00 sec 128 KBytes 1.05 Mbits/sec 0.466 ms 0/16 (0%)
[ 4] 5.00-6.00 sec 128 KBytes 1.05 Mbits/sec 0.456 ms 0/16 (0%)
[ 4] 6.00-7.00 sec 128 KBytes 1.05 Mbits/sec 0.452 ms 0/16 (0%)
[ 4] 7.00-8.00 sec 128 KBytes 1.05 Mbits/sec 0.447 ms 0/16 (0%)
[ 4] 8.00-9.00 sec 128 KBytes 1.05 Mbits/sec 0.451 ms 0/16 (0%)
[ 4] 9.00-10.00 sec 128 KBytes 1.05 Mbits/sec 0.460 ms 0/16 (0%)
- - - - - - - - - - - - - - - - - - - - - - - - -
Test Complete. Summary Results:
[ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
[ 4] 0.00-10.00 sec 1.26 MBytes 1.06 Mbits/sec 0.460 ms 0/161 (0%)
[ 4] Sent 161 datagrams
CPU Utilization: local/receiver 0.5% (0.0%u/0.5%s), remote/sender 0.0% (0.0%u/0.0%s)
iperf Done.
如在“间隔”列中所见,如果我手动运行相同的命令,大约每秒打印一行。我是 python 的新手,所以任何错误都是可能的。我尝试了其他几种缓存标准输出的方法,但它们也会像这样卡住输出。这能以某种方式解决吗?
BR安德烈亚斯
编辑:我认为问题是 iperf 没有刷新,但由于它显然每秒都在写一个新行,所以必须有一种方法可以在刷新之前捕获它。当运行更长的测试时,我注意到 stdout 缓冲区最终会达到最大值并且会刷新许多行并继续直到它再次满。
最佳答案
尝试将 --forceflush
命令行标志添加到 iperf3。这会导致 iperf3 在每行输出后刷新其输出。
关于python popen 标准输出卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097583/
我正在使用java引擎来处理一些内容。 我想知道这样会占用不必要的资源吗? command = 'some command to run my jar file' p = subprocess.Pop
我正在尝试使用以下代码块将一个进程的输出作为输入发送到另一个进程: p1 = Popen(command, stdout=PIPE) p2 = Popen(dgxmcmd, stdin=p1.stdo
popen 缓冲输出而系统没有。这是唯一的区别吗? 我知道 popen 和 system 都通过 shell 运行命令。但是,popen() 是否为 evil作为系统()? 最佳答案 看,从本质上讲,
用代码最容易解释: require 'timeout' puts "this block will properly kill the sleep after a second" IO.popen("
似乎既执行子进程又创建管道进行输入/输出,只是 subprocess 较新。 我的问题是,有没有subprocess.Popen可以做而os.popen不能做的功能,所以我们需要新模块subproce
我有一个生成以下输出的程序: ┌───────────────────────┐ │10 day weather forecast│
我正在使用以下命令来运行 shell 命令(创建子进程): cmd = "ls" process = subprocess.Popen(cmd, shell=True, stdout=subproce
得到结果后,我需要停止通过Python中的Popen发出的服务(在另一个线程的后台运行),但是以下方法失败了(只是使用ping)解释): class sample(threading.Thread):
Python - os.popen 和 subprocess.Popen 有什么区别? 最佳答案 os 进程功能被认为已过时。 subprocess 模块是在 Python 2.4 中引入的,作为与子
根据 python 文档 http://docs.python.org/library/subprocess.html ,建议将 os.popen 替换为 Popen 类,现在我有以下命令: impo
非常具体的问题(我希望):以下三个代码有什么区别? (我希望它只是第一个不等待子进程完成,而第二个和第三个会这样做。但我需要确定这是 only 的区别...) 我也欢迎其他评论/建议(尽管我已经很清楚
我有以下代码: pwd = '/home/user/svnexport/Repo/' updateSVN = "svn up " + pwd cmd = os.popen(updateSVN) get
我正在尝试编写简单的 c 函数,这将使我有可能看到从一个流到另一个流的数据传输进度,并将其显示在我的字符 LCD 上。 我设法传输数据并指示进度,但如何获得管道的结果? 所以基本上我想在 c 中做对应
我正在尝试使用 subprocess 模块与使用 Python 的命令行聊天机器人进行通信。 (http://howie.sourceforge.net/使用编译后的 win32 二进制文件,我有我的
我需要为 Ghostscript 创建一个 Monkey 补丁,我必须从 os.popen 迁移到 subsession.popen 因为我不能在我的系统中使用 shell . 我这样试过: def
在 Linux 操作系统上,下面的 python 代码提供了当前目录中的目录。 dirs = os.popen('ls -d */').read().split(os.linesep) print d
当我们从 Python 2.7.3 升级到 Python 2.7.5 时,大量使用 subprocess.Popen() 的内部库的自动化测试开始失败。该库用于线程环境。调试问题后,我能够创建一个简短
我无法得到它与 bash 相关或 python 子进程,但结果不同: >>> subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.
这里我想执行一个命令,我必须在执行第一个命令后给这个命令输入。 我想执行 obex_test蓝牙模式命令而不是在我必须为启动服务器提供像's'这样的输入之后我怎么能给这个东西。这是我的代码,我写了这个
在我的 Lua 程序中,我必须捕获来自外部程序的输出。这个外部程序需要某些环境变量。所以我这样做: e = "" e = e .. "A=100;" e = e .. "B=Hi;" e = e ..
我是一名优秀的程序员,十分优秀!