【python】多进程多线程

时间:2021-09-28 07:55:10
import threading
import multiprocessing class MultiThread(threading.Thread):
def __init__(self,func,args,name = ''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
self.res = None
def getResult(self):
if self.res == None:
self.res = False
return self.res
def run(self):
self.res = self.func(*self.args)
print '====================' class Test:
def __init__(self):
print '----Begin-线程---'
def t(self,i):
print u'线程:',i class MultT:
def __init__(self):
print '==========Begin-进程---'
def t(self,flag):
print u'进程:',flag
threads = []
for i in range(5):
t = MultiThread(Test().t, (i,))
threads.append(t)
for n in range(len(threads)):
threads[n].start()
for n in range(len(threads)):
threads[n].join() '''
class MultTest:
def __init__(self):
print '+++++++Begin+++++++'
def run(self):
jobs = []
for i in range(5):
p = multiprocessing.Process(target=MultT().t, args=(i,))
jobs.append(p) for j in jobs:
j.start()
for j in jobs:
j.join()
'''
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=MultT().t, args=(i,))
jobs.append(p) for j in jobs:
j.start()
for j in jobs:
j.join() #MultTest().run()