gpt4 book ai didi

python - "RuntimeError: This event loop is already running";在python 3.6.5中调试aiohttp,asyncio和IDE "spyder3"

转载 作者:太空狗 更新时间:2023-10-30 01:11:27 27 4
gpt4 key购买 nike

我正在努力理解为什么我会收到“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())

教程摘要的 结果(从spyder IDE运行代码时):

RuntimeError:此事件循环已在运行
<!doctype html>"

...(更多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 = []
print(data)
return(data)

async def get_async_urls(urls):
async with aiohttp.ClientSession() as client:
return await asyncio.gather(*(fetch(client, url) for url in urls))

if __name__ == '__main__':
t0 = time.time()
loop = asyncio.get_event_loop()
results = loop.run_until_complete(get_async_urls(urls))
print(results)
t1 = time.time()
total_time = t1-t0
loop.close()

个人摘要的 结果(从spyder IDE运行代码时):

RuntimeError:此事件循环已在运行

{“ask_price”:“14.9000”,“ask_size”:100,“bid_price”:“14.0100”,“bid_size”:100,“last_trade_price”:“14.7900”,“last_extended_hours_trade_price”:“14.7900”,“previous_close”:“14.3600“,” adjusted_previous_close“:” 14.3600“,” previous_close_date“:” 2018-05-07“,” symbol“:” SURF“,” trading_halted“:false,” has_traded“:true,” last_trade_price_source“:”合并“,“updated_at”:“2018-05-08T20:01:21Z”,“乐器”:“ https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/”}

个人代码段的 结果(从cmd“python personal_snippet.py”运行时):

{“ask_price”:“14.9000”,“ask_size”:100,“bid_price”:“14.0100”,“bid_size”:100,“last_trade_price”:“14.7900”,“last_extended_hours_trade_price”:“14.7900”,“previous_close”:“14.3600“,” adjusted_previous_close“:” 14.3600“,” previous_close_date“:” 2018-05-07“,” symbol“:” SURF“,” trading_halted“:false,” has_traded“:true,” last_trade_price_source“:”合并“,“updated_at”:“2018-05-08T20:01:21Z”,“乐器”:“ https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/”}
['{“ask_price”:“14.9000”,“ask_size”:100,“bid_price”:“14.0100”,“bid_size”:100,“last_trade_price”:“14.7900”,“last_extended_hours_trade_price”:“14.7900”,“previous_close” :“14.3600”,“adjusted_previous_close”:“14.3600”,“previous_close_date”:“2018-05-07”,“symbol”:“SURF”,“trading_halted”:false,“has_traded”:true,“last_trade_price_source”:“合并“,” updated_at“:” 2018-05-08T20:01:21Z“,”工具“:” https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/“}']

以上结果似乎表明该问题与Spyder IDE有关。

我有两个问题:
  • 为什么会出现此错误?
    似乎其他人在运行教程代码时都没有收到此错误。 (可能的答案:SPYDER3中可能出现的错误)

    这似乎仅在spyder IDE中发生。我从cmd提示符中运行了两个代码段,但没有出现错误。感谢@MikhailGerasimov的建议。
  • 假设我有两个打印命令(在第二段代码中)并且只打印了一组“数据”,那么为什么数据不返回原始调用(结果= loop.run_until_complete(get_async_urls(urls )))(可能的答案:SPYDER3中可能出现的错误)

    这似乎仅在spyder IDE中发生。我从cmd提示符中运行了第二段代码,并且两张照片都出现了。感谢@MikhailGerasimov的建议。




  • 两个问题都(可能)已答复。当我向spyder IDE提交问题时,我将引用此问题。随着事情在事物的发展方面,我将继续进行更新。如果他们回来说问题不在于他们,那么我将以略有不同的格式重新提出问题。

    谢谢您的帮助!



    链接到Spyder IDE的github中所引用的问题提交:
    https://github.com/spyder-ide/spyder/issues/7096

    最佳答案

    我对Spyder有相同的问题,唯一对我有用的解决方案是使用nest_asyncio

    使用以下命令安装nest_asyncio

    pip install nest_asyncio

    在文件中添加以下行
    import nest_asyncio
    nest_asyncio.apply()

    而且这个问题必须解决。

    从文档的

    By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it’s impossible to run tasks and wait for the result. Trying to do so will give the error “RuntimeError: This event loop is already running”.

    The issue pops up in various environments, such as web servers, GUI applications and in Jupyter notebooks.

    This module patches asyncio to allow nested use of asyncio.run and loop.run_until_complete.

    关于python - "RuntimeError: This event loop is already running";在python 3.6.5中调试aiohttp,asyncio和IDE "spyder3",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50243393/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com