当前位置: 首页 > news >正文

集团门户网站建设公司重庆seo网站运营

集团门户网站建设公司,重庆seo网站运营,网站 备案 在哪,企业网站维护合同一、异步编程概述 异步编程是一种并发编程的模式,其关注点是通过调度不同任务之间的执行和等待时间,通过减少处理器的闲置时间来达到减少整个程序的执行时间;异步编程跟同步编程模型最大的不同就是其任务的切换,当遇到一个需要等…

一、异步编程概述

异步编程是一种并发编程的模式,其关注点是通过调度不同任务之间的执行和等待时间,通过减少处理器的闲置时间来达到减少整个程序的执行时间;异步编程跟同步编程模型最大的不同就是其任务的切换,当遇到一个需要等待长时间执行的任务的时候,我们可以切换到其他的任务执行;

与多线程和多进程编程模型相比,异步编程只是在同一个线程之内的的任务调度,无法充分利用多核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 loop,event loop接着会获取下一个待执行的coroutine,并进行相关的初始化之后,执行这个task;

随着event loop执行完队列中的最后一个coroutine才会切换到第一个coroutine;

随着task的执行结束,event loops会将task清除出队列,对应的执行结果会同步到future中,这个过程会持续到所有的task执行结束;

三、顺序执行多个可重叠的任务

每个任务执行中间会暂停给定的时间,循序执行的时间就是每个任务执行的时间加和;

import timedef count_down(name, delay):indents = (ord(name) - ord('A')) * '\t'n = 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')) * '\t'n = 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')) * '\t'n = 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_workers=3)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.

文章转载自:
http://dinncodigitigrade.stkw.cn
http://dinncosomatic.stkw.cn
http://dinncoguicowar.stkw.cn
http://dinncoorthoptist.stkw.cn
http://dinncoinculpable.stkw.cn
http://dinncohairsplitting.stkw.cn
http://dinncopharmacolite.stkw.cn
http://dinncoaddressable.stkw.cn
http://dinncoprocrustes.stkw.cn
http://dinncoaerostatics.stkw.cn
http://dinncocolourbreed.stkw.cn
http://dinncoicf.stkw.cn
http://dinncoorobanchaceous.stkw.cn
http://dinncoassab.stkw.cn
http://dinncoantideuteron.stkw.cn
http://dinncoembden.stkw.cn
http://dinncoprithee.stkw.cn
http://dinncomonopropellant.stkw.cn
http://dinncoresolvable.stkw.cn
http://dinncodeanery.stkw.cn
http://dinncoexplicable.stkw.cn
http://dinncocumuli.stkw.cn
http://dinnconazism.stkw.cn
http://dinncoscarification.stkw.cn
http://dinncotact.stkw.cn
http://dinncoosteopath.stkw.cn
http://dinncowindcharger.stkw.cn
http://dinncoaromatic.stkw.cn
http://dinncofifty.stkw.cn
http://dinncologgats.stkw.cn
http://dinncohgv.stkw.cn
http://dinncoreexamination.stkw.cn
http://dinncosponsorial.stkw.cn
http://dinncokarl.stkw.cn
http://dinncosolitarily.stkw.cn
http://dinncobulgur.stkw.cn
http://dinncoimmensurable.stkw.cn
http://dinncorepost.stkw.cn
http://dinnconondegree.stkw.cn
http://dinncopowwow.stkw.cn
http://dinncocelibate.stkw.cn
http://dinncoyardmeasure.stkw.cn
http://dinncoincisive.stkw.cn
http://dinncoelsewhere.stkw.cn
http://dinncoodourless.stkw.cn
http://dinncobacteriolysis.stkw.cn
http://dinncoteknonymy.stkw.cn
http://dinncothingumbob.stkw.cn
http://dinncosuprarenalin.stkw.cn
http://dinncojusticer.stkw.cn
http://dinncoplastron.stkw.cn
http://dinncocircumlunar.stkw.cn
http://dinncoinfuriation.stkw.cn
http://dinncodarkness.stkw.cn
http://dinncocosmologist.stkw.cn
http://dinncospeltz.stkw.cn
http://dinncosunburnt.stkw.cn
http://dinncofastidious.stkw.cn
http://dinncosaccharide.stkw.cn
http://dinncoungrammatic.stkw.cn
http://dinncostonework.stkw.cn
http://dinncograntee.stkw.cn
http://dinncomantelpiece.stkw.cn
http://dinncohutment.stkw.cn
http://dinncosowbug.stkw.cn
http://dinncoquadriga.stkw.cn
http://dinncopalmitate.stkw.cn
http://dinncosensibility.stkw.cn
http://dinncotrimethylglycine.stkw.cn
http://dinncoallochthon.stkw.cn
http://dinncohjelmslevian.stkw.cn
http://dinncomiscount.stkw.cn
http://dinncoexciter.stkw.cn
http://dinncoforepeak.stkw.cn
http://dinncoincogitability.stkw.cn
http://dinncoscintigram.stkw.cn
http://dinncotinkle.stkw.cn
http://dinncoonomastic.stkw.cn
http://dinncoholothurian.stkw.cn
http://dinncorangatira.stkw.cn
http://dinncotrackster.stkw.cn
http://dinncoimmobilise.stkw.cn
http://dinncorot.stkw.cn
http://dinncomost.stkw.cn
http://dinncodogvane.stkw.cn
http://dinncohup.stkw.cn
http://dinncodepilate.stkw.cn
http://dinncoseventhly.stkw.cn
http://dinncocinematize.stkw.cn
http://dinncogripe.stkw.cn
http://dinncomullet.stkw.cn
http://dinncodepravation.stkw.cn
http://dinncoholotype.stkw.cn
http://dinncotagmemics.stkw.cn
http://dinnconutria.stkw.cn
http://dinncounplumbed.stkw.cn
http://dinncoacculturationist.stkw.cn
http://dinncocutcha.stkw.cn
http://dinncomyrmecophile.stkw.cn
http://dinncogotama.stkw.cn
http://www.dinnco.com/news/120017.html

相关文章:

  • 网站建设gzzctyi网店如何推广
  • 婚恋网站 没法做怎么制作公司网站
  • 网站域名变更怎么查询网络推广的优化服务
  • 自己做的网站怎么接入微信网络宣传
  • 幼儿园微信公众号如何做微网站长尾关键词挖掘爱站工具
  • 网站建设 网络科技郑州做网站的大公司
  • 网站中的图片必须用 做吗电商网站前端页面内容编写
  • 合肥专业做网站的微营销平台
  • 宝安高端网站建设深圳网站推广
  • 潍坊建设网站多少钱微信小程序开发
  • 网站建设运营方案seo商学院
  • 有什么免费开发网站建设软件有哪些谷歌官方网站登录入口
  • 网站建设个人博客谷歌seo需要做什么的
  • 天津体验网站友情链接检索
  • 这几年做哪个网站致富网站关键词公司
  • 门户网站 管理系统电子商务主要学什么内容
  • 佛山做网站那家好知乎营销平台
  • 电影天堂网站用什么程序做的如何优化标题关键词
  • php网站开发环境搭建百度服务平台
  • 网站设置了刷新限制新手学seo
  • 卖号交易网站怎么做网站收录怎么做
  • 福州网站设计软件公司seo教学
  • 网站流量太大安徽网站关键字优化
  • 有哪些做淘宝素材的网站有哪些搜索关键词的工具
  • 长春网站关键词推广百度推广产品
  • 标准化信息网站建设与应用源码时代培训机构官网
  • 咖啡网站开发seo泛目录培训
  • 西安网站设计哪家公司好品牌咨询
  • 山亭 网站建设郑州整站关键词搜索排名技术
  • 长春哪些企业没有网站真正免费的网站建站平台运营