4月28日 python学习总结 线程与协程
一、 异步与回调机制
问题:
1、任务的返回值不能得到及时的处理,必须等到所有任务都运行完毕才能统一进行处理
2、解析的过程是串行执行的,如果解析一次需要花费2s,解析9次则需要花费18s
解决一: (线程实现异步,回调解析结果)
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
from threading import current_thread
import requests
import os
import time
import random def get(url):
print('%s GET %s' %(current_thread().name,url))
response=requests.get(url)
time.sleep(random.randint(1,3)) if response.status_code == 200:
# 干解析的活
return response.text def pasrse(obj):
res=obj.result()
print('%s 解析结果为:%s' %(current_thread().name,len(res))) if __name__ == '__main__':
urls=[
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.python.org',
]
pool=ThreadPoolExecutor(4)
for url in urls:
obj=pool.submit(get,url) #放入进程池,实现异步操作
obj.add_done_callback(pasrse) #回调,将线程执行结果当作参数传递给pasrse函数,线程是谁先空闲谁执行结果处理,不存在主次之分 print('主线程',current_thread().name)
解决二: (进程实现异步,回调解析结果)
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
import requests
import os
import time
import random def get(url):
print('%s GET %s' %(os.getpid(),url))
response=requests.get(url)
time.sleep(random.randint(1,3)) if response.status_code == 200:
# 干解析的活
return response.text def pasrse(obj):
res=obj.result()
print('%s 解析结果为:%s' %(os.getpid(),len(res))) if __name__ == '__main__':
urls=[
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.python.org',
] pool=ProcessPoolExecutor(4)
for url in urls:
obj=pool.submit(get,url) #放入进程池,实现异步操作
obj.add_done_callback(pasrse) #回调,将进程执行结果当作参数传递给pasrse函数,由主进程执行 print('主进程',os.getpid())
二、线程queue
import queue q=queue.Queue(3) #队列:先进先出
q.put(1)
q.put(2)
q.put(3)
# q.put(4) print(q.get())
print(q.get())
print(q.get()) q=queue.LifoQueue(3) #堆栈:后进先出 q.put('a')
q.put('b')
q.put('c') print(q.get())
print(q.get())
print(q.get()) q=queue.PriorityQueue(3) #优先级队列:可以以小元组的形式往队列里存值,第一个元素代表优先级,数字越小优先级越高
q.put((10,'user1'))
q.put((-3,'user2'))
q.put((-2,'user3')) print(q.get())
print(q.get())
print(q.get())
三、线程Event
from threading import Event,current_thread,Thread
import time event=Event() # 监听信号 初始值为False def check():
print('%s 正在检测服务是否正常....' %current_thread().name)
time.sleep(5)
event.set() #set 方法将信号值 置为True def connect():
count=1
while not event.is_set(): #判断标记为是否为True
if count == 4:
print('尝试的次数过多,请稍后重试')
return
print('%s 尝试第%s次连接...' %(current_thread().name,count))
event.wait(1) #括号里的是等待时间,程序想继续运行,除非标志位为True或者超时,此处超时不会报错,是继续执行
count+=1
print('%s 开始连接...' % current_thread().name) if __name__ == '__main__':
t1=Thread(target=connect)
t2=Thread(target=connect)
t3=Thread(target=connect) c1=Thread(target=check) t1.start()
t2.start()
t3.start()
c1.start()
四、协程
1、单线程下实现并发:协程
并发指的多个任务看起来是同时运行的
并发实现的本质:切换+保存状态
2、并发、并行、串行:
并发:看起来是同时运行,切换+保存状态
并行:真正意义上的同时运行,只有在多cpu的情况下才能
实现并行,4个cpu能够并行4个任务
串行:一个人完完整整地执行完毕才运行下一个任务
import time
def consumer():
'''任务1:接收数据,处理数据'''
while True:
x=yield def producer():
'''任务2:生产数据'''
g=consumer()
next(g)
for i in range(10000000):
g.send(i) start=time.time()
#基于yield保存状态,实现两个任务直接来回切换,即并发的效果
#PS:如果每个任务中都加上打印,那么明显地看到两个任务的打印是你一次我一次,即并发执行的.
producer() #1.0202116966247559 stop=time.time()
print(stop-start)
并不是所有协程都能提升效率,如果是IO密集型的,协程会提高执行效率,然而计算密集型的切换并不能提高效率,反而会降低效率
五、单线程下实现遇到IO切换
1、greentlet可以切换,但不能遇到IO切
from greenlet import greenlet
import time def eat(name):
print('%s eat 1' %name)
time.sleep(30)
g2.switch('alex') #遇到switch切换
print('%s eat 2' %name)
g2.switch()
def play(name):
print('%s play 1' %name)
g1.switch()
print('%s play 2' %name) g1=greenlet(eat)
g2=greenlet(play) g1.switch('egon')
·
2、gevent切换,只能识别自己的IO操作,无法数别系统定义的IO,如time.sleep()
import gevent def eat(name):
print('%s eat 1' %name)
gevent.sleep(5) #gevent自定义的IO 可切换
print('%s eat 2' %name)
def play(name):
print('%s play 1' %name)
gevent.sleep(3)
print('%s play 2' %name) g1=gevent.spawn(eat,'egon')
g2=gevent.spawn(play,'alex') # g1.join()
# g2.join()
gevent.joinall([g1,g2]) #无法识别,不能切换
from gevent import monkey;monkey.patch_all()
import gevent
import time def eat(name):
print('%s eat 1' %name)
time.sleep(5) #无法识别,不能切换
print('%s eat 2' %name)
def play(name):
print('%s play 1' %name)
time.sleep(3)
print('%s play 2' %name) g1=gevent.spawn(eat,'egon')
g2=gevent.spawn(play,'alex') # g1.join()
# g2.join()
gevent.joinall([g1,g2])
3、若想要实现系统定义的IO切换需加上
import monkey;monkey.patch_all()
eg:
from gevent import monkey;monkey.patch_all()
from threading import current_thread
import gevent
import time def eat():
print('%s eat 1' %current_thread().name)
time.sleep(5)
print('%s eat 2' %current_thread().name)
def play():
print('%s play 1' %current_thread().name)
time.sleep(3)
print('%s play 2' %current_thread().name) g1=gevent.spawn(eat)
g2=gevent.spawn(play) # gevent.sleep(100)
# g1.join()
# g2.join()
print(current_thread().name)
gevent.joinall([g1,g2])
4月28日 python学习总结 线程与协程的更多相关文章
- 5月28日 python学习总结 CSS学习(二)
CSS属性相关 宽和高 width属性可以为元素设置宽度. height属性可以为元素设置高度. 块级标签才能设置宽度,内联标签的宽度由内容来决定. 字体属性 文字字体 font-family可以把多 ...
- 5月28日 python学习总结 CSS学习(一)
1. CSS是什么 层叠样式表 --> 给HTML添加样式的 2. CSS的语法 选择器 { 属性1:值1; 属性2:值2; } 3. CSS引入方式 1. 直接写在HTMl标签里面 <p ...
- Python—进程、线程、协程
一.线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务 方法: ...
- python 进程、线程与协程的区别
进程.线程与协程区别总结 - 1.进程是计算器最小资源分配单位 - 2.线程是CPU调度的最小单位 - 3.进程切换需要的资源很最大,效率很低 - 4.线程切换需要的资源一般,效率一般(当然了在不考虑 ...
- Python进程、线程、协程及IO多路复用
详情戳击下方链接 Python之进程.线程.协程 python之IO多路复用
- Python 进程、线程、协程、锁机制,你知多少?
1.python的多线程到底有没有用? 2. 为什么在python里推荐使用多进程而不是多线程 3.进程.线程.协程.各种锁 4.Python多进程编程
- Python进程、线程、协程的对比
1. 执行过程 每个线程有一个程序运行的入口.顺序执行序列和程序的出口.但是线程不能够独立执行,必须依存在进程中,由进程提供多个线程执行控制.每个线程都有他自己的一组CPU寄存器,称为线程的上下文,该 ...
- 4月2日 python学习总结
昨天内容回顾: 1.迭代器 可迭代对象: 只要内置有__iter__方法的都是可迭代的对象 既有__iter__,又有__next__方法 调用__iter__方法==>得到内置的迭代器对象 调 ...
- 4月8日 python学习总结 模块与包
一.包 #官网解释 Packages are a way of structuring Python's module namespace by using "dotted module n ...
随机推荐
- MATLAB基础学习篇(1)
MATLAB中只定义了以2和10为底对数,其它 使用换底公式,例如:log8(7)=log7/log8. x=input('Please enter x:'); y=input('Please ent ...
- GitHub page创建
这是我的博客,才刚刚建成请多多指教 https://henryztong.github.io/ 以下地址都是关于创建GitHub 博客的: 地址 说明 https://pages.github.c ...
- python语法_1基础语法概述
http://www.runoob.com/python3 章节:教程.基础语法.数据类型.解释器.注释.运算符. 大纲 查看python版本 实现第一个python3.x程序,hello world ...
- .NET 云原生架构师训练营(权限系统 代码实现 ActionAccess)--学习笔记
目录 开发任务 代码实现 开发任务 DotNetNB.Security.Core:定义 core,models,Istore:实现 default memory store DotNetNB.Secu ...
- Elasticsearch使用系列-Docker搭建Elasticsearch集群
Elasticsearch使用系列-ES简介和环境搭建 Elasticsearch使用系列-ES增删查改基本操作+ik分词 Elasticsearch使用系列-基本查询和聚合查询+sql插件 Elas ...
- 1、Linux基础--相关软件安装与网络配置
1.虚拟机(VM安装) 2.网络配置 3.Linux操作系统安装 4.xshell安装
- Unreal ListView使用篇
应用 ListView,在Unreal UI界面开发中用途非常广泛,基本只要你使用列表,就得需要用ListView.比如排行榜100个列表,界面上只需要显示出10个,我们肯定不能生成100个ui实例, ...
- 使用IWebBrowser2操控浏览器页面测试(IE)
测试一下在IE浏览器界面中插入代码测试,采用寻找窗口的方式获取Internet Explorer_Server句柄. 写的时候参考了很多网上的资料,有些地方不大适用就稍微修改了一下. Send ...
- Spring MVC参数绑定(如何接收请求参数及返回参数)
在SpringMVC interceptor案例实践中遇到了获取jsp表单传递参数失败的问题,怎么的解决的呢?下面详细介绍. 先讲述下https://www.cnblogs.com/ilovebath ...
- 『无为则无心』Python面向对象 — 45、面向对象编程
目录 1.面向对象编程的概念 2.面向对象编程和面向过程编程的区别 (1)面向过程编程 (2)面向对象编程 3.举例理解面向对象 4.Python的面向对象编程 5.面向对象的几大核心特性 1.面向对 ...