实验室网站建设方案,亚马逊电子商务网站的建设,建设工程合同管理论文,律师微网站建设一、异步编程概述
异步编程是一种并发编程的模式#xff0c;其关注点是通过调度不同任务之间的执行和等待时间#xff0c;通过减少处理器的闲置时间来达到减少整个程序的执行时间#xff1b;异步编程跟同步编程模型最大的不同就是其任务的切换#xff0c;当遇到一个需要等…一、异步编程概述
异步编程是一种并发编程的模式其关注点是通过调度不同任务之间的执行和等待时间通过减少处理器的闲置时间来达到减少整个程序的执行时间异步编程跟同步编程模型最大的不同就是其任务的切换当遇到一个需要等待长时间执行的任务的时候我们可以切换到其他的任务执行
与多线程和多进程编程模型相比异步编程只是在同一个线程之内的的任务调度无法充分利用多核CPU的优势所以特别适合IO阻塞性任务
python版本 3.9.5
二、python的异步框架模型
python提供了asyncio模块来支持异步编程其中涉及到coroutines、event loops、futures三个重要概念
event loops主要负责跟踪和调度所有异步任务编排具体的某个时间点执行的任务
coroutines是对具体执行任务的封装是一个可以在执行中暂停并切换到event loops执行流程的特殊类型的函数其一般还需要创建task才能被event loops调度
futures负责承载coroutines的执行结果其随着任务在event loops中的初始化而创建并随着任务的执行来记录任务的执行状态
异步编程框架的整个执行过程涉及三者的紧密协作
首先event loops启动之后会从任务队列获取第一个要执行的coroutine并随之创建对应task和future
然后随着task的执行当遇到coroutine内部需要切换任务的地方task的执行就会暂停并释放执行线程给event loopevent loop接着会获取下一个待执行的coroutine并进行相关的初始化之后执行这个task
随着event loop执行完队列中的最后一个coroutine才会切换到第一个coroutine
随着task的执行结束event loops会将task清除出队列对应的执行结果会同步到future中这个过程会持续到所有的task执行结束 三、顺序执行多个可重叠的任务
每个任务执行中间会暂停给定的时间循序执行的时间就是每个任务执行的时间加和
import timedef count_down(name, delay):indents (ord(name) - ord(A)) * \tn 3while n:time.sleep(delay)duration time.perf_counter() - startprint(- * 40)print(f{duration:.4f} \t{indents}{name} {n})n - 1start time.perf_counter()count_down(A, 1)
count_down(B, 0.8)
count_down(C, 0.5)
print(- * 40)
print(Done)# ----------------------------------------
# 1.0010 A 3
# ----------------------------------------
# 2.0019 A 2
# ----------------------------------------
# 3.0030 A 1
# ----------------------------------------
# 3.8040 B 3
# ----------------------------------------
# 4.6050 B 2
# ----------------------------------------
# 5.4059 B 1
# ----------------------------------------
# 5.9065 C 3
# ----------------------------------------
# 6.4072 C 2
# ----------------------------------------
# 6.9078 C 1
# ----------------------------------------
# Done四、异步化同步代码
python在语法上提供了async、await两个关键字来简化将同步代码修改为异步
async使用在函数的def关键字前边标记这是一个coroutine函数
await用在conroutine里边用于标记需要暂停释放执行流程给event loops
await 后边的表达式需要返回waitable的对象例如conroutine、task、future等
asyncio模块主要提供了操作event loop的方式
我们可以通过async将count_down标记为coroutine然后使用await和asyncio.sleep来实现异步的暂停从而将控制权交给event loop
async def count_down(name, delay, start):indents (ord(name) - ord(A)) * \tn 3while n:await asyncio.sleep(delay)duration time.perf_counter() - startprint(- * 40)print(f{duration:.4f} \t{indents}{name} {n})n - 1我们定义一个异步的main方法主要完成task的创建和等待任务执行结束
async def main():start time.perf_counter()tasks [asyncio.create_task(count_down(name,delay,start)) for name, delay in [(A, 1),(B, 0.8),(C, 0.5)]]await asyncio.wait(tasks)print(- * 40)print(Done)执行我们可以看到时间已经变为了执行时间最长的任务的时间了
asyncio.run(main())# ----------------------------------------
# 0.5010 C 3
# ----------------------------------------
# 0.8016 B 3
# ----------------------------------------
# 1.0011 A 3
# ----------------------------------------
# 1.0013 C 2
# ----------------------------------------
# 1.5021 C 1
# ----------------------------------------
# 1.6026 B 2
# ----------------------------------------
# 2.0025 A 2
# ----------------------------------------
# 2.4042 B 1
# ----------------------------------------
# 3.0038 A 1
# ----------------------------------------
# Done五、使用多线程克服具体任务的异步限制
异步编程要求具体的任务必须是coroutine也就是要求方法是异步的否则只有任务执行完了才能将控制权释放给event loop
python中的concurent.futures提供了ThreadPoolExecutor和ProcessPoolExecutor可以直接在异步编程中使用从而可以在单独的线程或者进程至今任务
import time
import asyncio
from concurrent.futures import ThreadPoolExecutordef count_down(name, delay, start):indents (ord(name) - ord(A)) * \tn 3while n:time.sleep(delay)duration time.perf_counter() - startprint(-*40)print(f{duration:.4f} \t{indents}{name} {n})n -1async def main():start time.perf_counter()loop asyncio.get_running_loop()executor ThreadPoolExecutor(max_workers3)fs [loop.run_in_executor(executor, count_down, *args) for args in [(A, 1, start), (B, 0.8, start), (C, 0.5, start)]]await asyncio.wait(fs)print(-*40)print(Done.)asyncio.run(main())# ----------------------------------------
# 0.5087 C 3
# ----------------------------------------
# 0.8196 B 3
# ----------------------------------------
# 1.0073 A 3
# ----------------------------------------
# 1.0234 C 2
# ----------------------------------------
# 1.5350 C 1
# ----------------------------------------
# 1.6303 B 2
# ----------------------------------------
# 2.0193 A 2
# ----------------------------------------
# 2.4406 B 1
# ----------------------------------------
# 3.0210 A 1
# ----------------------------------------
# Done.