2015-05-29 24 views
1

我爲跨平臺系統上的主機編寫了一個代碼。無法通過print逐行輸出(var,end ='')

代碼的內容如下:

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,但是失敗了結果在CentOS 6.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 

可以在任何蟒蛇專家幫我找出根本原因? 謝謝。

+0

你有幾個錯誤,我立刻看到:'在範圍X(5)''需要:'和'打印(完成)'應該是'打印( '完成')' – Scott

+0

嗨,斯科特。感謝您的回覆。這是錯字。我修好了它。但這不是失敗結果的根本原因。 – Pin

+0

我意識到,只是希望它是正確的。 – Scott

回答

1

這讓我想起了打印功能「緩衝」,然後一些數據後寫它的。 我在代碼中使用print時遇到了同樣的問題。爲了避免這樣的問題,我會用一個記錄器,這將立即打印代碼

import logging 
FORMAT = '%(asctime)s - %(levelname)s - %(message)s' 
logging.basicConfig(
    format=FORMAT, level=logging.DEBUG, datefmt='%Y/%m/%d %H:%M:%S') 
logger = logging.getLogger('MyLogger') 
logger.setLevel(logging.INFO) 


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) 
    logger.info(str(getLoading) + '<--') 

logger.info('done') 
+0

是的你是對的;有**會有一點緩衝繼續,所以*會在輸出中稍微延遲;但我不認爲這是OP本身的問題:) –

1

您需要在過程的stdout閱讀和有一點進行控制的,所以你可以做一些在線印刷的「流程負載」。

實施例:

#!/usr/bin/env python 

from __future__ import print_function 

import sys 
import psutil 
import subprocess 

try: 
    range = xrange 
except NameError: 
    pass 


p = subprocess.Popen(["ping", "-c", "5", "8.8.8.8"], stdout=subprocess.PIPE) 

encoding = sys.getdefaultencoding() 

for line in p.stdout: 
    load = psutil.cpu_percent(interval=1) 
    print("{0:s}{1:0.2f}<--".format(line.decode(encoding), load)) 
print("done") 

輸出:

$ ./foo.py 
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 
21.10<-- 
64 bytes from 8.8.8.8: icmp_seq=1 ttl=52 time=15.6 ms 
15.40<-- 
64 bytes from 8.8.8.8: icmp_seq=2 ttl=52 time=15.6 ms 
13.20<-- 
64 bytes from 8.8.8.8: icmp_seq=3 ttl=52 time=15.6 ms 
20.70<-- 
64 bytes from 8.8.8.8: icmp_seq=4 ttl=52 time=15.5 ms 
19.90<-- 
64 bytes from 8.8.8.8: icmp_seq=5 ttl=52 time=15.6 ms 
11.00<-- 

19.50<-- 
--- 8.8.8.8 ping statistics --- 
17.40<-- 
5 packets transmitted, 5 received, 0% packet loss, time 4007ms 
12.90<-- 
rtt min/avg/max/mdev = 15.596/15.629/15.669/0.114 ms 
16.60<-- 
done 

NB:這是爲Python 2/3兼容性寫入。

+0

我得到'print(「{0:s} {1:0.2f} < - 」。format(line,load)) TypeError:傳遞給對象的非空格式字符串.__ format__' – Scott

+1

對不起修復了這個問題;查看更新。原因是''line''是一個''bytes''字符串,所以需要解碼,因爲我們使用了''str''和''str.format()''。 –

+0

非常酷詹姆斯。不知道你爲什麼得到-1,但+1教我一些很酷的東西。 – Scott