我有如下代碼:Python的時間函數
import subprocess
def run_command(command):
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while p.poll() is None:
print "Loading mWorker... please wait"
time.sleep(2)
return p.communicate()
我原本以爲p.communicate()應該是之前while語句。但是我發現如果函數調用沒有放在方法的末尾,它會掛起(例如,我沒有看到任何超出返回提示的東西)。如果我運行如上所示的方法,我將只獲得1個打印語句,然後
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "python.py", line 10, in run_command
time.sleep(2)
NameError: global name 'time' is not defined
爲什麼我看到這個?爲什麼沒有時間工作多次?我需要做什麼改變,所以我會繼續看到打印聲明?
'進口時間'? – Hyperboreus
時間不會工作,甚至一次......你打印的東西,然後你用'time.sleep'命中,但python不知道'time'是什麼,因爲你從來沒有導入它。 – mgilson
好的,mglison。謝謝! – strangenewstar