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