注意事项:使用thread模块必须提供有效机制使主线程等待子线程执行结束后才退出,例如使用同步原语,或者使用sleep等待机制,这也是thread模块的缺陷,python 官方强烈建议不要使用thread模块,改用threading
thread 模块常用 API:
thread模块简单案例:
import thread
from time import sleep,ctime
loops = [4,2]
def loop(nloop,nsec,lock):
print 'start loop',nloop,'at:',ctime()
sleep(nsec)
print('loop',nloop,'done at:',ctime())
lock.release()
def main():
print 'starting at:',ctime()
locks=[]
nloops = range(len(loops))
for i in nloops:
lock = thread.allocate_lock()
lock.acquire()
locks.append(lock)
for i in nloops:
thread.start_new_thread(loop,(i,loops[i],locks[i]))
for i in nloops:
while locks[i].locked():
pass
print 'all done at :',ctime()
if __name__ == '__main__':
main()
threading模块:
简单案例:
import threading
from time import sleep,ctime
loops = [4,2]
def loop(nloop,nsec):
print 'start loop',nloop,'at :',ctime()
sleep(nsec)
print 'loop',nloop,'done at:',ctime()
def main():
print 'starting at :',ctime()
threads = []
nloops = range(len(loops))
for i in nloops:
t = threading.Thread(target=loop,args=(i,loops[i]))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all done at:',ctime()
if __name__ == '__main__':
main()
案例2:创建一个thread实例,传递一个可调用的类对象
import threading
from time import sleep,ctime
loops = [4,2]
class ThreadFunc(object):
def __init__(self,func,args,name=''):
self.name = name
self.func = func
self.args = args
def __call__(self):
apply(self.func,self.args)
def loop(nloop,nsec):
print 'start loop',nloop,'at:',ctime()
sleep(nsec)
print 'loop',nloop,'done at:',ctime()
def main():
print 'starting at:',ctime()
threads = []
nloops = range(len(loops))
for i in nloops:
t = threading.Thread(target=ThreadFunc(loop,(i,loops[i]),
loop.__name__))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all done at:',ctime()
if __name__ == '__main__':
main()
案例3:从thread 派生一个子类,创建一个子类的实例,让子类执行操作:
import threading
from time import sleep,ctime
loops = (4,2)
#创建自己的线程类,继承thread
class MyThread(threading.Thread):
def __init__(self,func,args,name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
#重载父类(threading.Thread的run方法)
def run(self):
apply(self.func,self.args)
def loop(nloop,nsec):
print 'start loop',nloop,'at:',ctime()
sleep(nsec)
print 'loop',nloop,'done at:',ctime()
def main():
print 'starting at:',ctime()
threads = []
nloops = range(len(loops))
for i in nloops:
t = MyThread(loop,(i,loops[i]),loop.__name__)
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all done at:',ctime()
if __name__ == '__main__':
main()
案例4:
import threading
from time import sleep,ctime
class MyThread(threading.Thread):
def __init__(self,func,args,name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def getResult(self):
return self.res
def run(self):
print 'starting',self.name,'at:',ctime()
self.res = apply(self.func,self.args)
print self.name,'finished at:',ctime()
def fib(x):
sleep(0.005)
if x < 2:
return 1
else:
return (fib(x-2)+fib(x-1))
def fac(x):
sleep(0.1)
if x < 2:
return 1
else:
return (x*fac(x-1))
def sum(x):
sleep(0.1)
if x < 2:
return 1
else:
return (x+sum(x-1))
funcs = (fib,fac,sum)
n = 12
def main():
nfuncs = range(len(funcs))
for i in nfuncs:
print 'starting',funcs[i].__name__,'at:',ctime()
print funcs[i](n)
print funcs[i].__name__,'finished at:',ctime()
threads = []
for i in nfuncs:
t = MyThread(funcs[i],(n,),funcs[i].__name__)
threads.append(t)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
print threads[i].getResult()
print 'all done'
if __name__ == '__main__':
main()