Python 单线程 多线程
来源:互联网 发布:python 爬虫 动态页面 编辑:程序博客网 时间:2024/06/05 07:06
早期的操作系统处理问题都是单任务的,同时只能执行一个任务,按顺序执行。
#coding=utf-8import threadingfrom time import ctime,sleep,clockdef music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(1)def movie(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(5) def surfInternet(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(4) def talk(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(3) if __name__ == '__main__': start = clock() music(u'南方姑娘') movie(u'战狼2') surfInternet(u'阿里巴巴') talk(u'使用wechat') end = clock() print "all over %s" %ctime() print "total time is %s" %(end-start)
I was listening to 南方姑娘. Wed Aug 30 15:35:16 2017
I was at the 战狼2! Wed Aug 30 15:35:17 2017
I was at the 战狼2! Wed Aug 30 15:35:22 2017
I was at the 阿里巴巴! Wed Aug 30 15:35:27 2017
I was at the 阿里巴巴! Wed Aug 30 15:35:31 2017
I was at the 使用wechat! Wed Aug 30 15:35:35 2017
I was at the 使用wechat! Wed Aug 30 15:35:38 2017
all over Wed Aug 30 15:35:41 2017
total time is 26.0033179014
#coding=utf-8import threadingfrom time import ctime,sleep,clockdef music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(1)def movie(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(5) def surfInternet(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(4) def talk(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(3)threads = []t1 = threading.Thread(target=music,args=(u'南方姑娘',))threads.append(t1)t2 = threading.Thread(target=movie,args=(u'战狼2',))threads.append(t2)t3 = threading.Thread(target=surfInternet,args=(u'阿里巴巴',))threads.append(t3)t4 = threading.Thread(target=talk,args=(u'使用wechat',))threads.append(t4)if __name__ == '__main__': start = clock() for t in threads: t.setDaemon(True) t.start() t.join()#在子线程完成运行之前,这个子线程的父线程将一直被阻塞。 end = clock() print "all over %s" %ctime() print "total time is %s" %(end-start)
I was at the 战狼2! Wed Aug 30 15:36:41 2017
I was at the 阿里巴巴! Wed Aug 30 15:36:41 2017
I was at the 使用wechat! Wed Aug 30 15:36:41 2017
I was listening to 南方姑娘. Wed Aug 30 15:36:42 2017
I was at the 使用wechat! Wed Aug 30 15:36:44 2017
I was at the 阿里巴巴! Wed Aug 30 15:36:45 2017
I was at the 战狼2! Wed Aug 30 15:36:46 2017
all over Wed Aug 30 15:36:47 2017
total time is 6.00455547207
从单线程的26s,减少到多线程的6s,节约20s时间。
threads = []
t1 = threading.Thread(target=music,args=(u'南方姑娘',))
threads.append(t1)
创建了threads数组,创建线程t1,使用threading.Thread()方法,在这个方法中调用music方法target=music,args方法对music进行传参。 把创建好的线程t1装到threads数组中。接着以同样的方式创建线程t2,t3,t4,并把t2,t3,t4也装到threads数组。
for t in threads:
t.setDaemon(True)
t.start()
最后通过for循环遍历数组。(数组被装载了t1,t2,t3和t4四个线程)
setDaemon()
setDaemon(True)将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。子线程启动后,父线程也继续执行下去,当父线程执行完最后一条语句print "all over %s" %ctime()后,没有等待子线程,直接就退出了,同时子线程也一同结束。
start()
开始线程活动。
join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。
- Python 单线程 多线程
- python 单线程和多线程
- python -- 单线程、多线程SocketServer
- python 单线程与多线程爬虫
- python多线程(1)--单线程和多线程
- python多线程获取网络数据,与单线程进行比较
- Python--网络爬虫单线程与多线程的比较
- python多线程与单线程之间的差距
- Python 单线程与多线程批量下载的比较
- Python中多线程和单线程端口扫描对比
- 多线程,单线程
- 单线程 多线程
- 单线程,多线程
- 单线程和多线程
- 单线程和多线程
- 单线程与多线程
- 单线程与多线程
- 多线程单线程
- Tomcat Connector运行模式从bio方式改成nio方式
- oracle 备忘
- Prime Number(CodeForces
- js中的apply和call的用法
- 剑指Offer—58—对称的二叉树
- Python 单线程 多线程
- 系统级性能分析工具 — Perf
- C语言中,#include<>和#include" "的区别
- 使用数据库创建的Flash 游戏- Pronto Move Shard
- Chrome Extension manifest【谷歌浏览器扩展之配置】
- fork一个子进程会拷贝父进程的什么
- Dump文件
- SpringMVC返回值类型
- 2017第20届济南国际机床展览会会刊(参展商名录)