【发布时间】:2020-05-14 17:31:31
【问题描述】:
我有一个大型遗留应用程序,它具有 一个 函数,该函数是异步执行的主要候选者。它受 IO 限制(网络和磁盘)并且不返回任何内容。 这是一个非常简单的类似实现:
import random
import time
import requests
def fetch_urls(site):
wait = random.randint(0, 5)
filename = site.split("/")[2].replace(".", "_")
print(f"Will fetch {site} in {wait} seconds")
time.sleep(wait)
r = requests.get(site)
with open(filename, "w") as fd:
fd.write(r.text)
def something(sites):
for site in sites:
fetch_urls(site)
return True
def main():
sites = ["https://www.google.com", "https://www.reddit.com", "https://www.msn.com"]
start = time.perf_counter()
something(sites)
total_time = time.perf_counter() - start
print(f"Finished in {total_time}")
if __name__ == "__main__":
main()
我的最终目标是更新 something 函数以异步运行 fetch_urls。 我无法更改 fetch_urls。
我能找到的所有文档和教程都假定我的整个应用程序是异步的(从 async def main() 开始),但事实并非如此。 这是一个跨越多个模块的巨大应用程序,并且为单个函数重构所有内容看起来不正确。
据我所知,我需要创建一个循环,向其中添加任务并以某种方式调度它,但我尝试了许多不同的方法,但我仍然让所有东西一个接一个地运行——而不是同时运行。
如有任何帮助,我将不胜感激。谢谢!
【问题讨论】:
标签: asynchronous