Python提供了几个用于多线程编程的模块,包括thread、threading和Queue等。thread和thread允许程序员创建和管理线程。thread模块提供了基本的线程和锁的支持,而threading提供了更高级别,功能更强的线程管理的功能。Queue模块允许用户创建一个可以用于多个线程之间共享数据的队列数据结构。
thread模块
thread模块处理产生线程外,也提供了基本的同步数据结构锁对象。
from time import sleep,ctime import thread 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 'start 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模块
threading模块不仅提供了Thread类,还提供了各种非常好用的同步机制。用Thread类,可以用多种方法来创建线程,在这里介绍三种不叫相像的方法。
创建一个Thread的实例,传给它一个函数
from time import sleep,ctime import threading loops=[4,2] def loop(nloop,nsec): print 'start loop ',nloop,'at :',ctime() sleep(nsec) print 'loop ',nloop,'done at :',ctime() def main(): print 'start 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()
创建一个Thread的实例,传给它一个可调用的类对象
from time import sleep,ctime import threading 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 'start 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()
从Thread派生出一个子类,创建一个这个子类的实例
from time import sleep,ctime import threading loops=[4,2] class MyThread(threading.Thread): def __init__(self,func,args,name=''): threading.Thread.__init__(self) self.name = name; self.func=func; self.args=args; 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 'start 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()
Queue模块
Queue模块可以用来进行线程间通信,让各个线程之间共享数据。现在,我们创建一个队列
from time import sleep import threading from random import randint from Queue import Queue class MyThread(threading.Thread): def __init__(self,func,args,name=''): threading.Thread.__init__(self) self.name = name; self.func=func; self.args=args; def run(self): apply(self.func,self.args) def writeQ(queue): print 'producing object for Q ...' queue.put('xxx',1) print 'size now',queue.qsize() def readQ(queue): val=queue.get(1) print 'sonsumed object form Q ... size now ',queue.qsize() def write(queue,loops): for i in range(loops): writeQ(queue) sleep(randint(1,3)) def read(queue,loops): for i in range(loops): readQ(queue) sleep(randint(2,5)) funcs=[write,read] nfuncs=range(len(funcs)) def main(): nloops=randint(2,5) q=Queue(32) threads=[] for i in nfuncs: t=MyThread(funcs[i],(q,nloops),funcs[i].__name__) threads.append(t) for i in nfuncs: threads[i].start() for i in nfuncs: threads[i].join() print 'all done' if __name__ == '__main__': main()