python-3.x - 尝试用 Python 理解 asyncio
问题描述
我正在尝试使用 asyncio 运行一些并发任务。目前,我得到了以下示例:
import asyncio
from time import sleep
from signal import SIGINT, SIGTERM, signal
async def f():
print("Got in F")
await asyncio.sleep(10)
print("Finished Sleep in F")
return "f"
async def g():
print("Got in G")
await asyncio.sleep(20)
print("Finished Sleep in G")
return "g"
async def count_timer():
for i in range(20):
print(i)
sleep(1)
async def main():
task_g = asyncio.create_task(g())
task_f = asyncio.create_task(f())
await task_g
await task_f
task_counter = asyncio.create_task(count_timer())
await task_counter
return
if __name__ == "__main__":
import time
s = time.perf_counter()
asyncio.run(main())
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
我想做的是在调用 f 和 g 函数之后调用 counter_timer 函数,但仍然同时运行所有三个函数。
提前谢谢你,卢卡斯·德尔菲诺·诺盖拉。
解决方案
推荐阅读
- maven - 未在本地复制的依赖项(depclean 警告)
- wpf - 如何从选项卡控件中删除白色边框?
- python - 仅使用 FBProphet 进行逐年预测的问题
- docker - 使用 DockerFile 构建 Git 子树
- http-proxy - Sencha cmd 6.5 不接受代理设置
- file-locking - 如何使用flock创建文件锁并稍后解锁?
- cmd - 如何使用命令提示符运行共享驱动器上可用的 exe
- java - 我在 Netbeans IDE 上运行 JSP 和 Servlet 时遇到问题
- python - python中pdf文件中图像的文本识别
- php - 我想让我的单选按钮粘在我的 php 表单中?