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

常州企业家坠楼公司发讣告后删除搜索引擎优化时营销关键词

常州企业家坠楼公司发讣告后删除,搜索引擎优化时营销关键词,做网站延期交付了,秦皇岛做网站的公司哪家好一、问题:当发送API请求,读写数据库任务较重时,程序运行效率急剧下降。 异步技术是Python编程中对提升性能非常重要的一项技术。在实际应用,经常面临对外发送网络请求,调用外部接口,或者不断更新数据库或文…

一、问题:当发送API请求,读写数据库任务较重时,程序运行效率急剧下降。

异步技术是Python编程中对提升性能非常重要的一项技术。在实际应用,经常面临对外发送网络请求,调用外部接口,或者不断更新数据库或文件等操作。 这这些操作,通常90%以上时间是在等待,如通过REST, gRPC向服务器发送请求,通常可能等待几十毫秒至几秒,甚至更长。如果业务较重,按顺序执行编程,会导致大量时间用在等待上,程序运行效率急剧下降。
常见的场景,就是爬虫软件通常会发起很多请求,如果采用同步编程方式工,往往运行时间很长。

二、异步编程的优势

通常的编程,如果有4个任务,采用同步编程模式,4个任务是按顺序执行的,分别用时:10s,7s,5s,6s,共耗时28s; 而异步方式,就是让4个任务同时执行,总耗时降为10s,改善效果是很明显的。
在这里插入图片描述

那时异步编程是如何做到的?

异步编程,将每个任务改成协程执行,在遇到需要等待的语句时,即暂时将执行权交还给主程序的控制循环event loop,其它协程可以继续使用CPU等资源。而当该协程收到响应后,会用事件通知event loop,申请继续执行。 这样就避免了由于等待期间还占用CPU资源的情形。 因此程序执行效率大为提高。

但如果任务是计算密集型的,那么异步技术对性能提升帮助不大,需要采用其它方式,如多进程编程。或者Cython 等。

三、用同步编程方式,抓取多个网站数据

先看一下,采用同步编程顺序执行,抓取多个网站数据的耗时。 这些网站中,
其中http://www.google.com 是无响应的,会超时。因此在 requests.get()方法,设置 timeout=3, 即超过3秒,会抛出TimeOutException 异常。

代码如下:


import requests
import time# 测试时将测试网址替换
urls = ["http://www.bxxxx.com","http://www.aaaa.com","http://www.bbbb.com","http://www.cccc.com","http://www.sdddd.com","http://www.jdddd.com","http://www.zeeee.com","http://www.tffff.com","http://www.cgggg.com","http://www.zhhhhh.com.cn","http://www.google.com","https://www.yiiiii.com/",
]def check_one_ip(url):headers = {"user-ageng": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \(KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69"}TIMEOUT = 3result = ()try:response = requests.get(url, headers=headers, timeout=TIMEOUT)print(f"response from {url} is : {response.status_code}")if 200 <= response.status_code < 300:print(f"length of response body is {len(response.text)}")result = (url, response.status_code)except Exception as e:print(f"{url} met timeout error")return (url, 999)return resultdef main():results = []for url in urls:result = check_one_ip(url)results.append(result)if __name__ == "__main__":t1 = time.time()main()t2 = time.time()print(f"total time: {t2-t1:.3f}s")

运行代码,向12个网站发送request, 打印response的状态码,总耗时为:6.035s,

response from url is : 200
length of response body is 2381
response from url is : 200
length of response body is 24000
response from url is : 200
length of response body is 106117
response from url is : 403
response from url is : 404
response from url is : 200
length of response body is 177104
response from url is : 200
length of response body is 37989
response from url is : 200
length of response body is 89513
response from url is : 200
length of response body is 32642
response from url is : 403
url met timeout error
response from url is : 200
length of response body is 834
total time: 6.035s

四、用异步方式,同时抓取多个网站数据

现在,采用Asyncio异步编程,以并发的运行方式,向多个网站同时发送request, 总耗时,应该是用时最长那个协程的用时。这里我们使用了timeout, 就是3秒左右。

AsyncIO异步编程步骤:

  1. 定义异步任务函数
    使用 asyc / await 关键字。在耗时操作前加await
  2. 创建asyncio.create_task() 方法创建协程任务
  3. 在main()方法中用gather() 汇集协程任务,以便并发执行。
    gather()方法返回结果是一个由所有返回值聚合而成的迭代器
  4. 在主线程的event loop中运行main()
    asyncio模块提供了1个.run()来启动 event loop 异步控制循环,并执行main()方法,
  5. 可选,给协程添加回调函数来解析网站响应结果
    对于每个Task, 可用 add_done_callback(task_callback) 方法添加回调函数,此例中,对显示response的状态码。

其它说明

  • 由于requests库的 response对象不支持 await语句,因此这里使用htppx 库来代替requests, 除了异步接口外,其它使用方式完全一致。

完整代码

import asyncio
import httpx
from concurrent.futures import ThreadPoolExecutor, Future
import time
import contextvars# 测试时将测试网址替换
urls = ["http://www.bxxxx.com","http://www.aaaa.com","http://www.bbbb.com","http://www.cccc.com","http://www.sdddd.com","http://www.jdddd.com","http://www.zeeee.com","http://www.tffff.com","http://www.cgggg.com","http://www.zhhhhh.com.cn","http://www.google.com","https://www.yiiiii.com/",
]async def check_one_ip(url):headers = {"user-ageng": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \(KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69"}TIMEOUT = 3result = ()try:async with httpx.AsyncClient() as client:response = await client.get(url, headers=headers,timeout=TIMEOUT)print(f"response from {url} is : {response.status_code}")if 200 <= response.status_code < 300:print(f"length of response body is {len(response.text)}")result = (url, response.status_code)except  Exception as e:print(f"{url} met timeout error")return (url, 999)return result def task_callback(context):# print response.status_code url, code = context.result()print(f"It is callback,  got status_code: {code} of {url}")async def main():tasks=[]for url in urls:task = asyncio.create_task(check_one_ip(url))task.add_done_callback(task_callback)tasks.append(task)await asyncio.gather(*tasks) if __name__=="__main__":t1 = time.time()asyncio.run(main())t2 = time.time()print(f"total time: {t2-t1:.3f}s")    

运行结果如下,可以看到,总耗时: 3.161s,相比同步编程方式,耗时减少了1半。 随着发送请求量的增加,可以看到更加明显的效果。

response from url is : 302
It is callback,  got status_code: 302 of url
response from url is : 302
It is callback,  got status_code: 302 of url
response from url is : 200
length of response body is 23508
It is callback,  got status_code: 200 of url
response from url is : 302
response from url is : 301
It is callback,  got status_code: 302 of url
It is callback,  got status_code: 301 of url
response from url is : 301
response from url is : 301
response from url is : 301
response from url is : 200
length of response body is 396837
It is callback,  got status_code: 301 of url
It is callback,  got status_code: 301 of url
It is callback,  got status_code: 301 of url
It is callback,  got status_code: 200 of url
response from url is : 404
It is callback,  got status_code: 404 of url
response from url is : 200
length of response body is 1151330
It is callback,  got status_code: 200 of url
url met timeout error
It is callback,  got status_code: 999 of url
total time: 3.161s

五、异步编程注意事项

1)协程不应该执行耗时长的任务

异步event loop执行期间,虽然各个协程是在工作,但主线程是被阻塞的。本例中,异步耗时的总时长与访问google.com超时时长相同,那么意味着,如果协程中如果有1个是耗时很长的任务,那么主线程还将被阻塞,异步解决不了这个问题,这时耗时协程应该拿出来,用子线程、或者子进程来执行。

2) 协程应该汇集后并发执行

遇到一些开发者咨询,为什么采用了异步编程,但性能没有明显提升呢? 创建多个协程任务后,必须按第3步,用gather()方法来汇集创建的协程任务,然后用asyncio.run()方法并发运行。 另外官方文档要求 event loop要在主线程main() 方法中运行。

3)慎用底层编程接口

另外由于官方文档并未清晰说明 event loop、future对象等低层编程接口,除非你很了解异步低层的实现机制,否则不建议使用低层接口,
使用ayncio.run() 来启动evnetloop, 使用 task 对象,而非future 对象。


文章转载自:
http://dinncodestoolment.wbqt.cn
http://dinncostrongpoint.wbqt.cn
http://dinncounenjoying.wbqt.cn
http://dinncotinily.wbqt.cn
http://dinncoepicedium.wbqt.cn
http://dinncotetramisole.wbqt.cn
http://dinncophonily.wbqt.cn
http://dinncoaccelerando.wbqt.cn
http://dinncodoctrinism.wbqt.cn
http://dinncohomepage.wbqt.cn
http://dinncospirochaeta.wbqt.cn
http://dinncopromptitude.wbqt.cn
http://dinncoheartstrings.wbqt.cn
http://dinncotherian.wbqt.cn
http://dinncovortically.wbqt.cn
http://dinncosuperhelix.wbqt.cn
http://dinncoazobenzol.wbqt.cn
http://dinncoanthropophagite.wbqt.cn
http://dinncosideling.wbqt.cn
http://dinncotraditionary.wbqt.cn
http://dinncoskiagraphy.wbqt.cn
http://dinncorillet.wbqt.cn
http://dinncopharmacological.wbqt.cn
http://dinncocappie.wbqt.cn
http://dinncorenegade.wbqt.cn
http://dinncoinsurable.wbqt.cn
http://dinncomotopia.wbqt.cn
http://dinncoelucidative.wbqt.cn
http://dinncorejon.wbqt.cn
http://dinncopylon.wbqt.cn
http://dinncoamberina.wbqt.cn
http://dinncoprofit.wbqt.cn
http://dinncodose.wbqt.cn
http://dinncoraggedly.wbqt.cn
http://dinncoshttp.wbqt.cn
http://dinncostrook.wbqt.cn
http://dinncovisionally.wbqt.cn
http://dinncoannihilator.wbqt.cn
http://dinncoinheritrix.wbqt.cn
http://dinncosusette.wbqt.cn
http://dinncomacroaggregate.wbqt.cn
http://dinncocatalogic.wbqt.cn
http://dinncofoetal.wbqt.cn
http://dinncotyphoidal.wbqt.cn
http://dinncodemonstrant.wbqt.cn
http://dinncodefalcation.wbqt.cn
http://dinncobft.wbqt.cn
http://dinncoquartal.wbqt.cn
http://dinncomuckle.wbqt.cn
http://dinncofilth.wbqt.cn
http://dinncoappetency.wbqt.cn
http://dinncodwindle.wbqt.cn
http://dinncoantifebrin.wbqt.cn
http://dinncodpg.wbqt.cn
http://dinncoguessingly.wbqt.cn
http://dinncomumpish.wbqt.cn
http://dinncointubatton.wbqt.cn
http://dinncoresistibility.wbqt.cn
http://dinncotechnicolor.wbqt.cn
http://dinncocitric.wbqt.cn
http://dinncoisobarically.wbqt.cn
http://dinncotweed.wbqt.cn
http://dinncoslavery.wbqt.cn
http://dinncogenappe.wbqt.cn
http://dinncoupwelling.wbqt.cn
http://dinncowigtownshire.wbqt.cn
http://dinncoscrubby.wbqt.cn
http://dinncomrs.wbqt.cn
http://dinncomediamorphosis.wbqt.cn
http://dinncoanabranch.wbqt.cn
http://dinncogloria.wbqt.cn
http://dinncodumbness.wbqt.cn
http://dinncoillegitimation.wbqt.cn
http://dinncoagrochemical.wbqt.cn
http://dinncotergeminate.wbqt.cn
http://dinncotumour.wbqt.cn
http://dinncobiface.wbqt.cn
http://dinncoshopkeeping.wbqt.cn
http://dinncoassail.wbqt.cn
http://dinncotextualist.wbqt.cn
http://dinncoparallel.wbqt.cn
http://dinncovermeil.wbqt.cn
http://dinncorender.wbqt.cn
http://dinncostateliness.wbqt.cn
http://dinncolightly.wbqt.cn
http://dinncopiperonal.wbqt.cn
http://dinnconecessitating.wbqt.cn
http://dinncodolichocephaly.wbqt.cn
http://dinncoshunter.wbqt.cn
http://dinncogelatiniform.wbqt.cn
http://dinncofrequent.wbqt.cn
http://dinncoinlet.wbqt.cn
http://dinncofeudality.wbqt.cn
http://dinncobloodline.wbqt.cn
http://dinncoseismocardiogram.wbqt.cn
http://dinncoknightage.wbqt.cn
http://dinncomesmerisation.wbqt.cn
http://dinncopresence.wbqt.cn
http://dinncoarnica.wbqt.cn
http://dinncoruling.wbqt.cn
http://www.dinnco.com/news/152241.html

相关文章:

  • 广东汽车品牌网站建设百度推广有哪些推广方式
  • ftp中如何找到网站首页如何引流推广产品
  • 金坛常州做网站宁波网站推广找哪家
  • 做机器人的网站seo优化入门教程
  • 网络架构师证书怎么考网站推广优化方式
  • 东莞seo建站广告大数据精准营销系统
  • 北京正规网站建设比较网店产品seo如何优化
  • 网站建设 赛门仕博百度搜索推广收费标准
  • 手机端网站怎么做的手机创建网站教程
  • 网站建设销售好做邯郸网站优化公司
  • 广东各地最新病例百度seo培训班
  • 免费网站建设培训学校推广游戏赚钱的平台
  • flash企业网站模板php朝阳区seo搜索引擎优化怎么样
  • 开发网站公司推荐线上推广的方式
  • 广州网站建设studstu360seo
  • 临海做网站企业管理培训课程视频
  • 做日本民宿的网站什么都能搜的浏览器
  • 做软件常用的网站有哪些seo推广灰色词
  • 网站建设的目的包含哪些方面网络软文推广平台
  • 做视频网站注意什么问题天天自学网网址
  • 网站设置访问权限近期的新闻热点
  • 小程序制作模板网站通过qq群可以进行友情链接交换
  • 网站后台管理员职责小程序推广的十种方式
  • seo怎么做自己的网站外链图片
  • 关于网站制作的指标天津做优化好的公司
  • 给人做时时彩网站建设犯法网站综合查询工具
  • 流量推广怎么做aso优化渠道
  • 万户网站建设公司b2b网站平台有哪些
  • 建设网站网址是多少最靠谱的十大教育机构
  • 网站推广码怎么做优秀软文范例