如何在Windows上检测正在运行的脚本?

时间:2013-08-06 18:00:19

标签: python

我在Windows后台运行一个简单的脚本。 我会在发生某些事情时启动它。我可以检测脚本是否正在运行?

import threading
def sayhello():
    print "hello world"
    global t        #Notice: use global variable!
    t = threading.Timer(5.0, sayhello)
    t.start()

t = threading.Timer(5.0, sayhello)
t.start()

1 个答案:

答案 0 :(得分:2)

您可以在线程上使用isAlive方法来检查它是否已在运行。在类似的方面,如果您正在运行一个进程,那么每次启动进程时都可以编写一个pid文件,并在需要时随时向操作系统查询该特定pid的状态。

有关isAlive的更多信息,请查看以下链接。

http://docs.python.org/2/library/threading.html#threading.Thread.isAlive