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

厦门外贸网站制作网站小图标

厦门外贸网站制作,网站小图标,东阳哪里可以做网站,深圳市住房和建设网站基础知识 在 Python 中,async 和 await 是用于异步编程的关键字,引入了异步/协程(coroutine)的概念。核心思想是通过 协程(Coroutine) 和 事件循环(Event Loop) 实现非阻塞并发&…

基础知识

在 Python 中,async 和 await 是用于异步编程的关键字,引入了异步/协程(coroutine)的概念。核心思想是通过 协程(Coroutine) 和 事件循环(Event Loop) 实现非阻塞并发,避免线程切换的开销。

异步编程是一种处理并发任务的方式,使得程序能够在等待某些 I/O 操作(如文件读写、网络请求等)的同时继续执行其他任务,而不会发生阻塞

  • 异步(Asynchronous):在异步编程中,程序不会等待某些I/O操作完成,而是继续执行其他任务,待操作完成后再回来处理结果
  • 协程(Coroutine):协程是一种轻量级的线程,可以在执行过程中暂停并让出控制权,然后在需要时恢复执行使用协程可以更有效地利用系统资源,避免线程切换的开销

异步和多线程区别

异步和多线程的区别可参考这篇文章:

一篇文章,搞懂异步和多线程的区别-腾讯云开发者社区-腾讯云 

同步示例

import datetime
import timeimport requestsdef result(url):res = request_url(url)def request_url(url):res = requests.get(url)print(url)time.sleep(2)print("execute_time:", datetime.datetime.now() - start_time)return resdef main():url_list = ["https://www.csdn.net/","https://blog.csdn.net/TomorrowAndTuture/article/details/149402464","https://www.baidu.com/",]task = [result(url) for url in url_list]start_time = datetime.datetime.now()
print(f"start_time:{start_time}\n")
main()
end_time = datetime.datetime.now()
cost_time = end_time - start_time
print("\nend_time:", end_time)
print("all_execute_time:", cost_time)
start_time:2025-07-16 17:33:00.905345https://www.csdn.net/
execute_time: 0:00:02.404500
https://blog.csdn.net/qq_43380180/article/details/111573642
execute_time: 0:00:04.839844
https://www.baidu.com/
execute_time: 0:00:07.543495end_time: 2025-07-16 17:33:08.448840
all_execute_time: 0:00:07.543495

异步示例

import asyncio
import datetimeimport requestsasync def result(url):res = await request_url(url)async def request_url(url):res = requests.get(url)print(url)await asyncio.sleep(2)print("execute_time:", datetime.datetime.now() - start_time)return resasync def main():url_list = ["https://www.csdn.net/","https://blog.csdn.net/TomorrowAndTuture/article/details/149402464","https://www.baidu.com/",]task = [asyncio.create_task(result(url)) for url in url_list]await asyncio.gather(*task)start_time = datetime.datetime.now()
print(f"start_time:{start_time}\n")
asyncio.run(main())
end_time = datetime.datetime.now()
cost_time = end_time - start_time
print("\nend_time:", end_time)
print("all_execute_time:", cost_time)
start_time:2025-07-16 17:38:26.561693https://www.csdn.net/
https://blog.csdn.net/qq_43380180/article/details/111573642
https://www.baidu.com/
execute_time: 0:00:03.136231
execute_time: 0:00:03.963955
execute_time: 0:00:04.945443end_time: 2025-07-16 17:38:31.508156
all_execute_time: 0:00:04.946463

asyncio.create_task() 用于并发执行多个协程任务,而 asyncio.gather() 用于等待多个协程任务的全部完成,并且可以收集执行结果。

关键步骤详解 

  • asyncio.run(coro):启动事件循环并运行协程。
  • asyncio.create_task(coro):将协程包装为 Task,加入事件循环并发执行。
  • asyncio.gather(*coros):并发执行多个协程,返回结果列表。
  • asyncio.sleep(delay):非阻塞等待(模拟 I/O 操作),如果是直接用 time.sleep(delay) 则会产生阻塞等待。
  • 使用 async def 定义协程,用 await 挂起阻塞操作。
  • 通过 asyncio.create_task() 和 asyncio.gather() 实现并发。
  • 避免在 async def 定义的协程中调用阻塞同步代码(比如 time.sleep(delay))。
http://www.dtcms.com/wzjs/617176.html

相关文章:

  • 企业网站建设价格表网站建设术语名词
  • 中山学校的网站建设网络营销方式和技巧
  • 宁波网站建设培训哪家好潍坊网站建设设计
  • 世界上最有趣的网站动态购物网站
  • 交互型网站网站建设与规划总结怎么写
  • 网站推广神器网站项目建设所需成本
  • wordpress建站难不难权威发布新冠疫苗接种禁忌
  • 梧州网站设计推荐昊诺网站建设网络推广
  • 网站前台修改昆明网络建站公司
  • 网站备案证书下载密码忘了毛网商城
  • 河南网站seo营销多少费用人才网站建设cms
  • 岳阳网站建设 熊掌号河北建网站
  • 秦皇岛城乡住房建设厅网站手机网站 焦点图
  • 深圳建设网站龙岗网站建设具有价值的广州做网站
  • 专业做包装的电商网站昆凌做的广告买化妆品网站
  • 专为中年人做的的婚恋网站做羞羞的事的视频网站
  • app网站建设一般多少钱网站域名查询网址
  • 珠海网站制作渠道宁夏建设学院官方网站
  • 版面设计网站有哪些ip查询网站
  • 网站建设waocc广州微信网站开发
  • 网站如何判断做的好不好网络推广公司司
  • 购买了网站如何使用吗广州网站设计专注乐云seo
  • 做产品封面的网站大学专业建设的内容
  • 建个人网站的详细步骤企业多语言网站开发
  • 外贸推广用中文网站wordpress 移动端m
  • 做实验流程图的网站网站自动采集指标
  • 做公众号主页面的有哪些网站晓风彩票门户网站建设
  • 做网站十大公司哪家好wordpress 第三方登录插件
  • 关键词优化排名易下拉软件河北seo推广系统
  • 百度站长平台开绿色收录通道加快网站收录做一家电商网站需要多少钱