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

python一些小细节

GIL锁:

当python使用多线程的时候, 每个线程通过请求这个锁获取运行权。 结束时归还


async/await/asyncio/gather/create_task/ThreadPool

理解asynciohttps://www.bilibili.com/video/BV1oa411b7c9?spm_id_from=333.788.videopod.sections&vd_source=8ca92588511fc633026e558331f021cb

装饰的function,

coroutine object

真正的并行和协程: 2s执行结束

async def main(): task1 = asyncio.create_task(say_after(1, "hello")) task2 = asyncio.create_task(say_after(2, "world")) print("start at", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) await task1 await task2 print("end at", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
串行, 3s执行结束
async def main(): print("start at", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) await say_after(1, "hello") await say_after(2, "world") print("end at", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

如果使用gather方式, 不需要单独create task再放进去(伪协程)

async def main(): print("start at", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) res = asyncio.gather( say_after(1, "hello"), say_after_2(2, "world") ) print(res) print("end at", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

threadpool

with ThreadPoolExecutor(max_workers=2) as executor: # 存储Future对象与自定义数据的映射 future_to_params = {} for task_id in range(1, 4): delay = task_id # 任务1延迟1秒,任务2延迟2秒,任务3延迟3秒 future = executor.submit(task, delay) future_to_params[future] = (task_id, delay) # 关联任务ID和延迟 # 按完成顺序处理 for future in as_completed(future_to_params): task_id, delay = future_to_params[future] try: result = future.result() print(f"任务ID:{task_id},{result}") except Exception as e: print(f"任务ID:{task_id},执行失败:{e}")

VizTracer:

Viztracer开发者讲解https://www.bilibili.com/video/BV1d34y1C78W?spm_id_from=333.788.player.switch&vd_source=8ca92588511fc633026e558331f021cb&p=2

基础用法:

装pip包

全局方式使用: python 1.py -> viztracer 1.py

内部函数方式使用:

with VizTracer(): func()

Perfetto(google 开源的trace)

Jupyter中使用:

# 初始load %load_ext viztracer # 使用时 %%viztracer
http://www.cnnetsun.cn/news/66360.html

相关文章:

  • 如何衡量推广效果(如投产比、转化率)?一位餐饮老板的实战自白
  • 程序员必看!万字长文详解大模型“深度研究“新范式,小白也能入门AI智能体开发!
  • 大模型安全威胁全解析,Agent架构设计避坑指南,小白必看
  • SMDJ45A单向 TVS瞬态抑制二极管 :3000W浪涌保护管 防雷击抗静电
  • Foundation 文本
  • Sui 主网升级至 V1.61.2
  • 25、Kubernetes 应用部署与管理实践
  • 31、容器化应用设计理念与实践
  • 如何评估LobeChat的加载速度与响应延迟?性能基准测试
  • 缓存与数据库一致性解决方案深度解析
  • 消息队列真仙:我的道念支持最终一致性
  • Spring Boot项目推送Gitee全流程(进阶)
  • Java毕设项目:基于Springboot大学校园自习室教室座位预约网站设计与实现基于springboot高校自习室预约系统的设计与实现(源码+文档,讲解、调试运行,定制等)
  • JAVA打造同城羽馆预约,一键畅享运动
  • 经验贴 | 科学制定招聘需求与预算:HR 必看的逻辑与实操要点
  • 经验贴 | AI 面试评估系统怎么用?HR 高效识人实操指南
  • 构建个性化AI助手:LobeChat会话管理功能深度使用技巧
  • 基于昇腾NPU的YOLOV8-seg c++部署
  • 26、深入探索脚本编程与系统安全基础
  • XSS漏洞有哪几种?DOM型XSS和反射型有什么区别?SQL注入原理又是什么?网安面试题常见问题一文详解
  • 压力扫描阀:并行校准技术,解锁多点压力测量新高度
  • PyTorch框架下运行Qwen3-32B的内存优化策略
  • 为什么说Qwen3-8B是学术研究的理想选择?实测报告出炉
  • java基础-PriorityQueue(优先队列)
  • Qwen3-14B模型量化压缩技术:降低GPU内存占用
  • 18、日期和时间的格式化、解析及时间区域的使用
  • VisionPro CogIPOneImageTool1 工具超详细解释(含内部功能全解析)
  • VisionPro CogIDTool 工具超深度详解(技术细节 + 实战配置版)
  • 让 BI 拥有‘领域大脑’:智能 BI 如何实现 AI 级精准数据查询
  • 提示工程架构师的战略规划:提示系统生命周期管理