我在跨平台系统上为ping主机编写了一个代码。你知道吗
代码内容如下:
import psutil
import subprocess
proc = subprocess.Popen(["ping -c 5 8.8.8.8"],shell=True)
for x in range(5):
getLoading = psutil.cpu_percent(interval=1)
print(str(getLoading),end='<--')
print('done')
我希望我能得到如下结果:
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
5.0<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
4.5<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
4.1<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
3.5<--
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
4.0<--
done
我在Windows7/python3.4.3上得到了预期的结果,但在centos6.5/python3.4.3上没有得到结果。 Linux上的结果如下所示:
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=5ms
5.0<--4.5<--4.1<--3.5<--4.0<--done
有哪位python专家能帮我找出根本原因吗? 谢谢。你知道吗
这让我想起了打印函数“缓冲”一些数据,然后再写入。 我在代码中使用print时也遇到过类似的问题。为了避免这种问题,我会使用一个记录器,这将立即打印代码
您需要读取进程的标准输出,并对其进行一些控制,这样您就可以在线打印“进程加载”。你知道吗
示例:
输出:
NB:这是为了与Python 2/3兼容而编写的。你知道吗
相关问题 更多 >
编程相关推荐