python多线程

来源:互联网 发布:日本经济泡沫 知乎 编辑:程序博客网 时间:2024/06/08 10:49
一般来说,python使用线程有两种模式:
(1)创建线程要执行的函数,把这个函数传递进Thread对象里,让它来执行;

(2)继承Thread类,创建一个新的class,将要执行的代码 写到run函数里面。

(1)方法:

#!/usr/bin/python# -*- coding: utf-8 -*-import threading,timedef test(loop, sec):    print 'start loop', loop, 'at:', time.ctime()    time.sleep(sec)    print 'loop', loop, 'done at:', time.ctime()def main():    print 'starting at:', time.ctime()    for i in xrange(10):        th = threading.Thread(target= test,args= (i,2))        th.start()        th.join()    print 'all Done at:', time.ctime()if __name__ == '__main__':        main()
结果:

starting at: Tue May  2 14:01:49 2017start loop 0 at: Tue May  2 14:01:49 2017loop 0 done at: Tue May  2 14:01:51 2017start loop 1 at: Tue May  2 14:01:51 2017loop 1 done at: Tue May  2 14:01:53 2017start loop 2 at: Tue May  2 14:01:53 2017loop 2 done at: Tue May  2 14:01:55 2017start loop 3 at: Tue May  2 14:01:55 2017loop 3 done at: Tue May  2 14:01:57 2017start loop 4 at: Tue May  2 14:01:57 2017loop 4 done at: Tue May  2 14:01:59 2017start loop 5 at: Tue May  2 14:01:59 2017loop 5 done at: Tue May  2 14:02:01 2017start loop 6 at: Tue May  2 14:02:01 2017loop 6 done at: Tue May  2 14:02:03 2017start loop 7 at: Tue May  2 14:02:03 2017loop 7 done at: Tue May  2 14:02:05 2017start loop 8 at: Tue May  2 14:02:05 2017loop 8 done at: Tue May  2 14:02:07 2017start loop 9 at: Tue May  2 14:02:07 2017loop 9 done at: Tue May  2 14:02:09 2017all Done at: Tue May  2 14:02:09 2017

(2)方法

#!/usr/bin/python# -*- coding: utf-8 -*-import threading,timeclass myThread (threading.Thread) :      def __init__(self, loop, sec) :          super(myThread, self).__init__()          self.loop = loop          self.sec = sec      def run(self):          print 'start loop', self.loop, 'at:', time.ctime()          time.sleep(self.sec)          print 'loop', self.loop, 'done at:', time.ctime()def main():      print 'starting at:', time.ctime()         for i in xrange(10):         th = myThread(i,2)         th.start()         th.join()         print 'all Done at:', time.ctime()if __name__ == '__main__':        main()
结果:

starting at: Tue May  2 14:13:17 2017start loop 0 at: Tue May  2 14:13:17 2017loop 0 done at: Tue May  2 14:13:19 2017start loop 1 at: Tue May  2 14:13:19 2017loop 1 done at: Tue May  2 14:13:21 2017start loop 2 at: Tue May  2 14:13:21 2017loop 2 done at: Tue May  2 14:13:23 2017start loop 3 at: Tue May  2 14:13:23 2017loop 3 done at: Tue May  2 14:13:25 2017start loop 4 at: Tue May  2 14:13:25 2017loop 4 done at: Tue May  2 14:13:27 2017start loop 5 at: Tue May  2 14:13:27 2017loop 5 done at: Tue May  2 14:13:29 2017start loop 6 at: Tue May  2 14:13:29 2017loop 6 done at: Tue May  2 14:13:31 2017start loop 7 at: Tue May  2 14:13:31 2017loop 7 done at: Tue May  2 14:13:33 2017start loop 8 at: Tue May  2 14:13:33 2017loop 8 done at: Tue May  2 14:13:35 2017start loop 9 at: Tue May  2 14:13:35 2017loop 9 done at: Tue May  2 14:13:37 2017all Done at: Tue May  2 14:13:37 2017





0 0