《python多线程就这么简单》由会员分享,可在线阅读,更多相关《python多线程就这么简单(8页珍藏版)》请在金锄头文库上搜索。
1、python多线程就这么简单单线程在好些年前的MS-DOS时代,操作系统处理问题都是单任务的,我想做听音乐和看电影两件事儿,那么一定要先排一下顺序。(好吧!我们不纠结在DOS时代是否有听音乐和看影的应用。_)复制代码from time import ctime,sleepdef music(): for i in range(2): print I was listening to music. %s %ctime() sleep(1)def move(): for i in range(2): print I was at the movies! %s %ctime() sleep(5)if
2、 _name_ = _main_: music() move() print all over %s %ctime()复制代码我们先听了一首音乐,通过for循环来控制音乐的播放了两次,每首音乐播放需要1秒钟,sleep()来控制音乐播放的时长。接着我们又看了一场电影,每一场电影需要5秒钟,因为太好看了,所以我也通过for循环看两遍。在整个休闲娱乐活动结束后,我看了一下当前时间,差不多该睡觉了。运行结果:复制代码= RESTART =I was listening to music. Thu Apr 17 10:47:08 2014I was listening to music. Thu Ap
3、r 17 10:47:09 2014I was at the movies! Thu Apr 17 10:47:10 2014I was at the movies! Thu Apr 17 10:47:15 2014all over Thu Apr 17 10:47:20 2014复制代码其实,music()和move()更应该被看作是音乐和视频播放器,至于要播放什么歌曲和视频应该由我们使用时决定。所以,对上面代码做了改造:复制代码#coding=utf-8import threadingfrom time import ctime,sleepdef music(func): for i in
4、 range(2): print I was listening to %s. %s %(func,ctime() sleep(1)def move(func): for i in range(2): print I was at the %s! %s %(func,ctime() sleep(5)if _name_ = _main_: music(u爱情买卖) move(u阿凡达) print all over %s %ctime()复制代码对music()和move()进行了传参处理。体验中国经典歌曲和欧美大片文化。运行结果:复制代码 = RESTART =I was listening
5、to 爱情买卖. Thu Apr 17 11:48:59 2014I was listening to 爱情买卖. Thu Apr 17 11:49:00 2014I was at the 阿凡达! Thu Apr 17 11:49:01 2014I was at the 阿凡达! Thu Apr 17 11:49:06 2014all over Thu Apr 17 11:49:11 2014复制代码多线程科技在发展,时代在进步,我们的CPU也越来越快,CPU抱怨,P大点事儿占了我一定的时间,其实我同时干多个活都没问题的;于是,操作系统就进入了多任务时代。我们听着音乐吃着火锅的不在是梦想。p
6、ython提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费你和时间,所以我们直接学习threading 就可以了。继续对上面的例子进行改造,引入threadring来同时播放音乐和视频:复制代码#coding=utf-8import threadingfrom time import ctime,sleepdef music(func): for i in range(2): print I was listening to %s. %s %(func,ctime() sleep(1)def move(fun
7、c): for i in range(2): print I was at the %s! %s %(func,ctime() sleep(5)threads = t1 = threading.Thread(target=music,args=(u爱情买卖,)threads.append(t1)t2 = threading.Thread(target=move,args=(u阿凡达,)threads.append(t2)if _name_ = _main_: for t in threads: t.setDaemon(True) t.start() print all over %s %cti
8、me()复制代码import threading首先导入threading 模块,这是使用多线程的前提。threads = t1 = threading.Thread(target=music,args=(u爱情买卖,)threads.append(t1)创建了threads数组,创建线程t1,使用threading.Thread()方法,在这个方法中调用music方法target=music,args方法对music进行传参。 把创建好的线程t1装到threads数组中。以同样的方式创建线程t2,并把t2也装到threads数组。for t in threads:t.setDaemon(Tr
9、ue)t.start()最后通过for循环遍历数组。setDaemon()setDaemon(True)将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。子线程启动后,父线程也继续执行下去,当父线程执行完最后一条语句print all over %s %ctime()后,没有等待子线程,直接就退出了,同时子线程也一同结束。start()开始线程活动。运行结果: = RESTART =I was listening to 爱情买卖. Thu Apr 17 12:51:45 2014 I was at the 阿凡达! Thu Apr 17 12:
10、51:45 2014 all over Thu Apr 17 12:51:45 2014从执行结果来看,子线程(muisc 、move )和主线程(print all over %s %ctime())都是同一时间启动,但由于主线程执行完结束,所以导致子线程也终止。继续调整程序:复制代码.if _name_ = _main_: for t in threads: t.setDaemon(True) t.start() t.join() print all over %s %ctime()复制代码我们只对上面的程序加了个join()方法,用于等待线程终止。join()的作用是,在子线程完成运行之
11、前,这个子线程的父线程将一直被阻塞。注意: join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程。运行结果:复制代码 = RESTART =I was listening to 爱情买卖. Thu Apr 17 13:04:11 2014 I was at the 阿凡达! Thu Apr 17 13:04:11 2014I was listening to 爱情买卖. Thu Apr 17 13:04:12 2014I was at the 阿凡达! Thu Apr 17 13:04:16 2014all over Thu Apr 17 1
12、3:04:21 2014复制代码从执行结果可看到,music 和move 是同时启动的。开始时间4分11秒,直到调用主进程为4分22秒,总耗时为10秒。从单线程时减少了2秒,我们可以把music的sleep()的时间调整为4秒。.def music(func): for i in range(2): print I was listening to %s. %s %(func,ctime() sleep(4).执行结果:复制代码 = RESTART =I was listening to 爱情买卖. Thu Apr 17 13:11:27 2014I was at the 阿凡达! Thu Apr 17 13:11:27 2014I was listening to 爱情买卖. Thu Apr 17 13:11:31 2014I was at the 阿凡达! Thu Apr 17 13:11:32 2014all over Thu Apr 17 13:11:37 2014