我正在使用aiohttp、asyncio和codetiming来发出并发请求。我最近将 Python 升级到 3.9.0 版,但出现 RuntimeError: 程序运行后事件循环已关闭。我正在尝试使用队列数据结构发出异步请求...
import asyncio
import aiohttp
from codetiming import Timer
async def task(name, work_queue):
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
async with aiohttp.ClientSession() as session:
while not work_queue.empty():
url = await work_queue.get()
print(f"Task {name} getting URL: {url}")
timer.start()
async with session.get(url) as response:
await response.text()
timer.stop()
async def main():
# Create the queue of work
work_queue = asyncio.Queue()
# Put some work in the queue
for url in …Run Code Online (Sandbox Code Playgroud)