您的位置:首页 > 编程语言 > Python开发

python 多线程查找进程pid

2013-01-16 14:49 169 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: