Python 之 threading
创建多线程常用的三种方法:
- 创建Thread的实例,传给它一个函数
- 创建Thread的实例,传给它一个可调用的类实例(不推荐)
- 派生Thread的子类,并创建子类的实例(推荐)
创建Thread的实例,传给它一个函数
1 #!/usr/bin/env python 2 # coding:utf-8 3 4 5 import threading 6 from time import ctime, sleep 7 8 loops = [4, 2] 9 10 def loop(n, sec): 11 print 'start loop', n, ' at: ', ctime() 12 sleep(sec) 13 print 'loop', n, ' done at: ', ctime() 14 15 def main(): 16 print 'starting at: ', ctime() 17 threads = [] 18 nloops = range(len(loops)) 19 20 for i in nloops: 21 t = threading.Thread(target=loop, args=(i, loops[i])) 22 threads.append(t) 23 24 for i in nloops: # start threads 25 threads[i].start() 26 27 for i in nloops: # wait for all threads to finish 28 threads[i].join() 29 30 print 'all DONE at: ', ctime() 31 32 if __name__ == '__main__': 33 main()
执行结果:
liuqian@ubuntu:~$ python test_threading1.py
starting at: Wed Jul 20 13:20:06 2016
start loop 0 at: Wed Jul 20 13:20:06 2016
start loop 1 at: Wed Jul 20 13:20:06 2016
loop 1 done at: Wed Jul 20 13:20:08 2016
loop 0 done at: Wed Jul 20 13:20:10 2016
all DONE at: Wed Jul 20 13:20:10 2016
【说明】
join([timeout])方法将等待线程结束,或者在提供了超时时间的情况下达到超时时间。对于join()方法而言,其实它根本不需要调用。一旦线程启动,它就会一直执行,直到给定的函数完成后退出。如果主线程还有其他事情要去做,而不是等待这些线程完成(例如其他处理或者等待新的客户端请求),就可以不调用join()。join()只在你需要等待线程完成的时候才是有用的。
创建Thread的实例,传给它一个可调用的类实例
1 #!/usr/bin/env python 2 # coding:utf-8 3 4 5 import threading 6 from time import ctime, sleep 7 8 loops = [4,2] 9 10 class ThreadFunc(object): 11 12 def __init__(self, func, args, name=''): 13 self.name = name 14 self.func = func 15 self.args = args 16 17 def __call__(self): 18 self.func(*self.args) 19 20 def loop(n, sec): 21 print 'start loop', n, ' at: ', ctime() 22 sleep(sec) 23 print 'loop', n, ' done at: ', ctime() 24 25 def main(): 26 print 'starting at: ', ctime() 27 threads = [] 28 nloops = range(len(loops)) 29 30 for i in nloops: 31 t = threading.Thread(target=ThreadFunc(loop, (i, loops[i]), loop.__name__)) 32 threads.append(t) 33 34 for i in nloops: # start threads 35 threads[i].start() 36 37 for i in nloops: # wait for all threads to finish 38 threads[i].join() 39 40 print 'all DONE at: ', ctime() 41 42 if __name__ == '__main__': 43 main()
输出与第一个案例一样。
【说明】当创建新线程时,Thread类的代码将调用ThreadFunc对象,此时会调用__cal__()这个特殊方法。
派生Thread的子类,并创建子类的实例(推荐)
1 #!/usr/bin/env python 2 # coding:utf-8 3 4 import threading 5 from time import ctime, sleep 6 7 loops = [4,2] 8 9 class MyThread(threading.Thread): 10 11 def __init__(self, func, args, name=''): 12 threading.Thread.__init__(self) 13 self.name = name 14 self.func = func 15 self.args = args 16 17 def run(self): # 必须要写 18 self.func(*self.args) 19 20 def loop(n, sec): 21 print 'start loop', n, ' at: ', ctime() 22 sleep(sec) 23 print 'loop', n, ' done at: ', ctime() 24 25 def main(): 26 print 'starting at: ', ctime() 27 threads = [] 28 nloops = range(len(loops)) 29 30 for i in nloops: 31 t = MyThread(loop, (i, loops[i]), loop.__name__) 32 threads.append(t) 33 34 for i in nloops: # start threads 35 threads[i].start() 36 37 for i in nloops: # wait for all threads to finish 38 threads[i].join() 39 40 print 'all DONE at: ', ctime() 41 42 if __name__ == '__main__': 43 main()