2017-03-29 16 views
1

私は多くの質問を読んで、J.F. Sebastian's Python 3 solution の応答を含むライブstdoutプリントに触れ、stdoutを読んでいます。しかしpython popen stdout freezing

、彼のソリューションは、このシナリオで動作している間:

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='') 

私は、私が実際に使用するアプリケーションで期待通りに動作しません:pingのシナリオについては

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='') 

、あらゆる私はそれを手動で実行したかのように行が表示されます。 iperfを使用すると、2行の出力の後で停止し、アプリケーションが完了したらすべてをフラッシュします。 私はスクリプトの中でお互いの後にそれらを実行した場合、私はこの出力を得る:インターバル欄に見ることができるように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. 

を完了した後THIS LINE AFTER

[email protected]:~/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 

EVERYTHINGが図示されています同じコマンドを手動で実行すると、1秒あたり約1行が印刷されます。私はPythonに新しいので、任意の間違いが可能です。私はstdoutをキャッシュする他の方法をいくつか試しましたが、このような出力もフリーズしています。これは何とか解決できますか?

BR アンドレアス

編集:私はフラッシュしないiperfのあることの問題について考えたが、それは明らかに新しい行を書いているので、毎秒はフラッシュの前にそれをキャッチする方法がなければなりません。より長いテストを実行すると、stdoutバッファーは最終的に最大値に達し、多くの行がフラッシュされ、再びフルになるまで続きます。

+0

あなたは['.readline()'アプローチを試しましたか(http://stackoverflow.com/a/1085100/223424)? – 9000

+0

@ 9000これはまったく同じ結果をもたらします。最初の2行は印刷され、それが停止し、すべてが終了するのを待ちます。 'iperf 3.0.7'の第1行' 'Linux raspbery ...' 2行目 – HAL

+0

'Popen'コンストラクタで' 'bufsize'を設定しようとしましたか(https://docs.python.org/3/library/subprocess.html) #subprocess.Popen)を0に設定しますか? – 9000

答えて

0

--forceflushコマンドラインフラグをiperf3に設定してみてください。これにより、iperf3は出力の各行の後に出力をフラッシュします。

関連する問題