I have code shown below:
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()
I originally thought the p.communicate() should be prior to the while statement. However I find that this function call will hang (e.g. I do not see anything past the return prompt) if it is not placed at the end of the method. If I run the method as shown above, I will only get 1 print statement and then
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
Why am I seeing this? Why doesn’t time work more than once? And what changes do I need to make so I will continue to see the print statement?
You have not imported the time module.