Python之路,Day9 - 线程、进程、协程和IO多路复用
参考博客:
线程、进程、协程:
http://www.cnblogs.com/wupeiqi/articles/5040827.html
http://www.cnblogs.com/alex3714/articles/5230609.html
IO多路复用:
http://www.cnblogs.com/wupeiqi/articles/5040823.html
课堂笔记:
- 线程进程介绍
1. 工作最小单元是线程
2. 应用程序 -> 至少有一个进程 -> 至少有一个线程
3. 应用场景:
IO密集型:线程
计算密集型:进程
4. GIL,全局解释器锁。
- 保证同一个进程中只有一个线程同时被调度
- 线程
1. 基本使用
def task(arg):
time.sleep(arg)
print(arg)
for i in range(5):
t = threading.Thread(target=task,args=[i,])
# t.setDaemon(True) # 主线程终止,不等待子线程
# t.setDaemon(False)
t.start()
# t.join() # 一直等
# t.join(1) # 等待最大时间
2. 锁
# 1. 只能有一个人使用锁
# lock = threading.Lock() # 只能开一把
# lock = threading.RLock()# 可以开多把
# 2. 多个人同时使用锁
# lock = threading.BoundedSemaphore(3)
# 3. 所有的解脱锁的限制
# lock = threading.Event()
# 4. 肆意妄为
# lock = threading.Condition()
3. 线程池
模式一:直接处理
def task(url):
"""
任务执行两个操作:下载;保存本地
"""
# response中封装了Http请求响应的所有数据
# - response.url 请求的URL
# - response.status_code 响应状态码
# - response.text 响应内容(字符串格式)
# - response.content 响应内容(字节格式)
# 下载
response = requests.get(url)
# 下载内容保存至本地
f = open('a.log','wb')
f.write(response.content)
f.close()
pool = ThreadPoolExecutor(2)
url_list = [
'http://www.oldboyedu.com',
'http://www.autohome.com.cn',
'http://www.baidu.com',
]
for url in url_list:
print('开始请求',url)
# 去连接池中获取链接
pool.submit(task,url)
模式二:分步处理
def save(future):
"""
只做保存 # future中包含response
"""
response = future.result()
# 下载内容保存至本地
f = open('a.log','wb')
f.write(response.content)
f.close()
def task(url):
"""
只做下载 requests
"""
# response中封装了Http请求响应的所有数据
# - response.url 请求的URL
# - response.status_code 响应状态码
# - response.text 响应内容(字符串格式)
# - response.content 响应内容(字节格式)
# 下载
response = requests.get(url)
return response
pool = ThreadPoolExecutor(2)
url_list = [
'http://www.oldboyedu.com',
'http://www.autohome.com.cn',
'http://www.baidu.com',
]
for url in url_list:
print('开始请求',url)
# 去连接池中获取链接
# future中包含response
future = pool.submit(task,url)
# 下载成功后,自动调用save方法
future.add_done_callback(save)
- 进程
1. 基本使用
from multiprocessing import Process
import time
def task(arg):
time.sleep(arg)
print(arg)
if __name__ == '__main__':
for i in range(10):
p = Process(target=task,args=(i,))
p.daemon = True
# p.daemon = False
p.start()
p.join(1)
print('主进程最后...')
2. 进程之间的数据共享
特殊的东西
- Array(‘类型’,长度)
- Manager().list() / Manager().dict()
3. 进程池
================== 结论 ==================
IO密集:线程
计算密集:进程
- 协程
pip3 install greenlet
协程永远是一个线程在执行,对线程的一个分片处理。
二次加工:
自定义:
select实现
现成 :
pip3 install gevent
- IO多路复用
监听多个socket对象是否有变化(可读,可写,发送错误)
- 示例一:
- socketserverIO
- IO多路复用
- 线程
- 自定义异步非阻塞的框架
本周作业:
服务端:socketserver
用IO多路复用select,使用“伪”并发
客户端:
基本操作:
聊天
上传
尝试:
客户端是否可以用select来实现???
Python之路,Day9 - 线程、进程、协程和IO多路复用的更多相关文章
- Python进程、线程、协程及IO多路复用
详情戳击下方链接 Python之进程.线程.协程 python之IO多路复用
- python之并发编程(线程\进程\协程)
一.进程和线程 1.进程 假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输入(I/O操作),而此时CPU只能静静地等待任务A读取完数据才能继续执行,这样就白白浪费了CPU资源.是 ...
- 进程,线程,协程,io多路复用 总结
并发:要做到同时服务多个客户端,有三种技术 1. 进程并行,只能开到当前cpu个数的进程,但能用来处理计算型任务 ,开销最大 2. 如果并行不必要,那么可以考虑用线程并发,单位开销比进程小很多 线程: ...
- Python学习笔记整理总结【网络编程】【线程/进程/协程/IO多路模型/select/poll/epoll/selector】
一.socket(单链接) 1.socket:应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socke ...
- 学到了林海峰,武沛齐讲的Day34 完 线程 进程 协程 很重要
线程 进程 协程 很重要 ...儿子满月回家办酒,学的有点慢,坚持
- 多线程、多进程、协程、IO多路复用请求百度
最近学习了多线程.多进程.协程以及IO多路复用,那么对于爬取数据来说,这几个方式哪个最快呢,今天就来稍微测试一下 普通方式请求百度5次 import socket import time import ...
- 文成小盆友python-num11-(1) 线程 进程 协程
本节主要内容 线程补充 进程 协程 一.线程补充 1.两种使用方法 这里主要涉及两种使用方法,一种为直接使用,一种为定义自己的类然后继承使用如下: 直接使用如下: import threading d ...
- python_21_线程+进程+协程
python_线程_进程_协程 什么是线程? -- os能够进行运算调度的最小单位,被包含在进程之中,是一串指令的集合 -- 每个线程都是独立的,可以访问同一进程下所有的资源 什么是进程? -- 每个 ...
- 12.python进程\协程\异步IO
进程 创建进程 from multiprocessing import Process import time def func(name): time.sleep(2) print('hello', ...
随机推荐
- C# 各种导入 Excel 文件的数据的方法总结
在导入之前都需要将上传的文件保存到服务器,所以避免重复的写这些代码,先贴出上传文件并保存到服务器指定路径的代码. protected void btnImport_Click(object sende ...
- 带你走进ajax(4)
处理ajax返回数据类型 ajax返回数据类型:纯文本格式.xml.json 如果只获取简单的字符串可以采用纯文本格式. 如果返回的数据类型比较复杂,则采用xml或者json. 采用XML来处理数据 ...
- Lucene.Net 3.0.3如何从TokenStream中获取token对象
Lucene.Net最高版本为3.0.3,并且apache已经不再提供Lucene.Net的更新,没仔细研究过Lucene.Net的所有版本,Lucene.Net3.0.3遍历TokenStream获 ...
- java中hashSet原理
转自: http://blog.csdn.net/guoweimelon/article/details/50804799 HashSet是JavaMap类型的集合类中最常使用的,本文基于Java1. ...
- CMake入门教程(转帖)
本文转自:https://www.cnblogs.com/never--more/p/6921837.html CMake入门教程 参考文献:http://www.ibm.com/developerw ...
- nfs挂载
安装: yum install nfs-utils rpcbind 配置共享目录:vim /etc/exports /xxx/cloudcms *(insecure,rw,async,no_root_ ...
- golang中文字符编码转换
golang 有很多需要将中文转成utf8的 网上搜到一个直接转的,记录下,备用 package main import "golang.org/x/text/encoding/simpli ...
- NSURLSession 的学习资料
一个nsurlsession的一些学习资料 http://www.cocoachina.com/ios/20161018/17785.html
- 手动装配Bean
代码: import org.springframework.context.ApplicationContext; import org.springframework.context.suppor ...
- You only look once
计算MAP https://www.zhihu.com/question/53405779 http://tarangshah.com/blog/2018-01-27/what-is-map-unde ...