Let's say that I have this simple line in python:
假设我在python中有这个简单的行:
os.system("sudo apt-get update")
of course, apt-get will take some time untill it's finished, how can I check in python if the command had finished or not yet?
当然,apt-get需要一些时间直到它完成,如果命令已经完成或者还没有完成,我怎么能检查python?
Edit: this is the code with Popen:
编辑:这是Popen的代码:
os.environ['packagename'] = entry.get_text()
process = Popen(['dpkg-repack', '$packagename'])
if process.poll() is None:
print "It still working.."
else:
print "It finished"
Now the problem is, it never print "It finished" even when it really finish.
现在的问题是,即使它真的完成,它也永远不会打印出“已完成”。
3 个解决方案
#1
7
As the documentation states it:
正如文档所述:
This is implemented by calling the Standard C function system(), and has the same limitations
这是通过调用标准C函数系统()来实现的,并且具有相同的限制
The C call to system
simply runs the program until it exits. Calling os.system
blocks your python code until the bash command has finished thus you'll know that it is finished when os.system
returns. If you'd like to do other stuff while waiting for the call to finish, there are several possibilities. The preferred way is to use the subprocessing module.
对系统的C调用只是运行程序直到它退出。调用os.system会阻塞你的python代码,直到bash命令完成,因此当os.system返回时你就会知道它已经完成了。如果你想在等待电话结束时做其他事情,有几种可能性。首选方法是使用子处理模块。
from subprocess import Popen
...
# Runs the command in another process. Doesn't block
process = Popen(['ls', '-l'])
# Later
# Returns the return code of the command. None if it hasn't finished
if process.poll() is None:
# Still running
else:
# Has finished
Check the link above for more things you can do with Popen
查看上面的链接,了解Popen可以做的更多事情
For a more general approach at running code concurrently, you can run that in another thread or process. Here's example code:
对于同时运行代码的更一般方法,您可以在另一个线程或进程中运行该方法。这是示例代码:
from threading import Thread
...
thread = Thread(group=None, target=lambda:os.system("ls -l"))
thread.run()
# Later
if thread.is_alive():
# Still running
else:
# Has finished
Another option would be to use the concurrent.futures
module.
另一种选择是使用concurrent.futures模块。
#2
0
os.system
is blocking; it calls the command waits for its completion, and returns its return code.
os.system阻塞;它调用命令等待它的完成,并返回它的返回码。
So, it'll be finished once os.system
returns.
所以,一旦os.system返回它就会完成。
If your code isn't working, I think that could be caused by one of sudo
's quirks, it refuses to give rights on certain environments(I don't know the details tho.).
如果你的代码不起作用,我认为这可能是由sudo的一个怪癖引起的,它拒绝在某些环境中给予权利(我不知道细节)。
#3
0
os.system
will actually wait for the command to finish and return the exit status (format dependent format).
os.system实际上将等待命令完成并返回退出状态(格式相关的格式)。
#1
7
As the documentation states it:
正如文档所述:
This is implemented by calling the Standard C function system(), and has the same limitations
这是通过调用标准C函数系统()来实现的,并且具有相同的限制
The C call to system
simply runs the program until it exits. Calling os.system
blocks your python code until the bash command has finished thus you'll know that it is finished when os.system
returns. If you'd like to do other stuff while waiting for the call to finish, there are several possibilities. The preferred way is to use the subprocessing module.
对系统的C调用只是运行程序直到它退出。调用os.system会阻塞你的python代码,直到bash命令完成,因此当os.system返回时你就会知道它已经完成了。如果你想在等待电话结束时做其他事情,有几种可能性。首选方法是使用子处理模块。
from subprocess import Popen
...
# Runs the command in another process. Doesn't block
process = Popen(['ls', '-l'])
# Later
# Returns the return code of the command. None if it hasn't finished
if process.poll() is None:
# Still running
else:
# Has finished
Check the link above for more things you can do with Popen
查看上面的链接,了解Popen可以做的更多事情
For a more general approach at running code concurrently, you can run that in another thread or process. Here's example code:
对于同时运行代码的更一般方法,您可以在另一个线程或进程中运行该方法。这是示例代码:
from threading import Thread
...
thread = Thread(group=None, target=lambda:os.system("ls -l"))
thread.run()
# Later
if thread.is_alive():
# Still running
else:
# Has finished
Another option would be to use the concurrent.futures
module.
另一种选择是使用concurrent.futures模块。
#2
0
os.system
is blocking; it calls the command waits for its completion, and returns its return code.
os.system阻塞;它调用命令等待它的完成,并返回它的返回码。
So, it'll be finished once os.system
returns.
所以,一旦os.system返回它就会完成。
If your code isn't working, I think that could be caused by one of sudo
's quirks, it refuses to give rights on certain environments(I don't know the details tho.).
如果你的代码不起作用,我认为这可能是由sudo的一个怪癖引起的,它拒绝在某些环境中给予权利(我不知道细节)。
#3
0
os.system
will actually wait for the command to finish and return the exit status (format dependent format).
os.system实际上将等待命令完成并返回退出状态(格式相关的格式)。