python 多线程查找进程pid

import os,time,threading

class Message(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name = name
    
    def printIt(self):
        # print 'checking process is : ' + self.num
        cmd = 'ps -C ' + self.name + ' -o pid,cmd'
        ret=os.popen(cmd).readlines()
        if len(ret) < 2:
            print '[' + self.name + '] process ' + ' process  not found'
                    # os.system('top')
        elif len(ret) >= 2:
            print '[' + self.name + '] process ' + ret[0].strip()[0:3] + ' is ' + ret[-1].strip()[:-3]
        else:
            print "other error"

def run(type):
    thread1 = Message(type[0])
    thread2 = Message(type[1])
    thread1.printIt()
    thread2.printIt()
if __name__ == '__main__':
    type=["top","aa"]
    run(type)

 

posted on 2013-01-16 14:49  wangbokun  阅读(860)  评论(0)    收藏  举报

导航