我很难理解为什么我得到"RuntimeError:此事件循环已经在运行"运行时错误.我试图从" https://aiohttp.readthedocs.io/en/stable/ " 运行代码片段但是,我一直遇到同样的问题.
教程中的代码片段:
import aiohttp
import asyncio
import async_timeout
async def fetch(session, url):
async with async_timeout.timeout(10):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://python.org')
print(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)
教程片段的结果(从spyder IDE运行代码时):
RuntimeError:此事件循环已在运行
<!doctype html>"
Run Code Online (Sandbox Code Playgroud)
...(更多HTML)
个人代码段(不是上面引用的教程):
import aiohttp
import asyncio
import time
urls = ['https://api.robinhood.com/quotes/c4f6720a-0364-4e7b-8c41-705696759e1a/']
async def fetch(client, url):
async with client.request('get', url) as response:
if response.status == 200:
data = await response.text()
else:
data …Run Code Online (Sandbox Code Playgroud)