2017-03-29 8 views
1

나는 stdout을 읽으려면 J.F. Sebastian's Python 3 solution 에서 대답을 포함하여 라이브 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='') 

그것은 작동하지 않습니다 실제로 사용하려면 :

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를 사용하면 두 줄의 출력이 끝난 후 중단되고 응용 프로그램이 완료되면 모든 내용이 플러시됩니다. I 스크립트 서로 후에 실행하면 는 I이 출력 얻을 : 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 

모든 것이 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. 
수동으로 동일한 명령을 실행하면 초당 약 한 줄이 인쇄됩니다. 나는 파이썬에 익숙하지 않으므로 어떤 실수도 가능하다. 나는 stdout을 캐싱하는 몇 가지 다른 방법을 시도했지만 이것 역시 출력을 멈추게한다. 어떻게 든 해결 될 수 있습니까?

BR 안드레아스

편집 : 나는 세척하지 문제가되는 iperf 생각하지만 분명 새로운 라인을 작성하기 때문에 모든 두 번째는 세척하기 전에 그것을 잡을 수있는 방법이 있어야합니다. 더 긴 테스트를 수행 할 때 stdout 버퍼가 결국 최대 값을 갖게되고 많은 라인이 플러시되고 다시 가득 찰 때까지 계속됩니다.

+0

['.readline()'접근법을 시도 했습니까? (http://stackoverflow.com/a/1085100/223424)? – 9000

+0

@ 9000 그러면 똑같은 결과가 나옵니다. 처음 두 줄이 인쇄되면 멈추고 모든 것이 끝날 때까지 기다립니다. 'iperf 3.0.7' 첫 번째 행''Linux raspbery ...'두 번째 줄 – HAL

+0

'Popen' 생성자에서 ['bufsize'를 설정하려고 했습니까?] (https://docs.python.org/3/library/subprocess.html) # subprocess.Popen)을 0으로 설정 하시겠습니까? – 9000

답변

0

iperf3에 --forceflush 명령 줄 플래그를 사용해보십시오. 이로 인해 iperf3는 각 출력 라인 이후에 출력을 플러시합니다.