Python检查进程仍然通过其名称运行

时间:2014-12-23 13:31:35

标签: python

我想知道如何查找具有特定名称的特定进程是否仍在运行?

import multiprocessing
from time import sleep

def proc():
    for i in range(10000):
        sleep(.05)



if __name__ == '__main__':
    multiprocessing.process.Process(target=proc, name='name1').start()
    multiprocessing.process.Process(target=proc, name='name2').start()
    multiprocessing.process.Process(target=proc, name='name3').start()
    multiprocessing.process.Process(target=proc, name='name4').start()

    #how i know in here process with name 'name1' is finiehsd or not ?

1 个答案:

答案 0 :(得分:1)

import multiprocessing
from time import sleep

def proc():
    for i in range(10000):
        sleep(.05)



if __name__ == '__main__':
    list = []
    for i in xrange(4):
        p = multiprocessing.process.Process(target=proc, name='name' + str(i))
        p.start()
        list.append(p)

    # Mainthread check which is still alive and which is not.
    while 1:
        for p in list:
            if not p.is_alive():
                print p.name + " is Done!"
                p.join()
                list.remove(p)

这是一个解决方案。