“RuntimeError:这个事件循环已经在运行";调试 aiohttp、asyncio 和 IDE“spyder3";在 python 3.6.5 中 [英] "RuntimeError: This event loop is already running"; debugging aiohttp, asyncio and IDE "spyder3" in python 3.6.5

查看:32
本文介绍了“RuntimeError:这个事件循环已经在运行";调试 aiohttp、asyncio 和 IDE“spyder3";在 python 3.6.5 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解为什么我会收到RuntimeError:此事件循环已经在运行"运行时错误.我试图从https://aiohttp.readthedocs.io/en/stable/运行代码片段"但是,我一直遇到同样的问题.

I'm struggling to understand why I am getting the "RuntimeError: This event loop is already running" runtime error. I have tried to run snippets of code from "https://aiohttp.readthedocs.io/en/stable/" however, I keep getting the same issue.

教程中的代码片段:

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: This event loop is already running

<!doctype html>"

...(更多 html)

... (more 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: This event loop is already running

{"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.0100","previous_price":"14.0100","790":"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":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"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":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"}

个人代码片段的结果(从 cmdpython 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.0100","previous_price":"14.0100","790":"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":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"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.0100","bid_size":"14.7900":"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","instrument":"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":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"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":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"}']

上述结果似乎表明问题与 Spyder IDE 相关.

我有两个问题:

  1. 为什么会出现此错误?其他人在运行教程代码时似乎没有收到此错误.(可能的回答:SPYDER3 中可能存在 BUG)

  1. Why am I getting this error? It seems like other people do not get this error when running the tutorial code. (Potentially answered: POSSIBLE BUG IN SPYDER3)

这似乎只发生在 spyder IDE 中.我从 cmd 提示符运行了两个代码片段,没有出现错误.感谢@MikhailGerasimov 的建议.

鉴于我有两个打印命令(在第二段代码中)并且只打印了一组数据",那么 为什么数据没有返回到原始调用(结果 =loop.run_until_complete(get_async_urls(urls)))(可能的回答是:SPYDER3 中可能存在 BUG)

Given that I have two print commands (in the second snippet of code) and that only one set of "data" was printed, then why is data not getting back to the original call (results = loop.run_until_complete(get_async_urls(urls))) (Potentially answered: POSSIBLE BUG IN SPYDER3)

这似乎只发生在 spyder IDE 中.我从 cmd 提示符运行了第二段代码,两个打印件都出现了.感谢@MikhailGerasimov 的建议.

<小时><小时><小时>

这两个问题(可能)都得到了回答.当我向 spyder IDE 提交问题时,我会参考这个问题.随着事物在 spyder 方面的发展,我将继续更新此内容.如果他们回来说问题不在于他们,那么我将以稍微不同的格式重新提出问题.




BOTH QUESTIONS HAVE (probably) BEEN ANSWERED. I will reference this question when I submit an issue to spyder IDE. I will continue to update this as things evolve on the spyder side of things. If they come back and say the issue does not lie with them, then I will reopen the questions in a slightly different format.

感谢您的帮助!

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

Link to the referenced issue submission in Spyder IDE's github: https://github.com/spyder-ide/spyder/issues/7096

推荐答案

我在使用 Spyder 时遇到了同样的问题,唯一对我有用的解决方案是使用 nest_asyncio

I have the same issue with Spyder, The only solution that worked for me was to use nest_asyncio

使用命令安装nest_asyncio

install the nest_asyncio by using the command

pip install nest_asyncio

在文件中添加以下几行

import nest_asyncio
nest_asyncio.apply()

问题必须得到解决.

来自文档的

按照设计 asyncio 不允许嵌套其事件​​循环.这提出了一个实际问题:当在事件循环已经运行的环境中时,不可能运行任务并等待结果.尝试这样做会给出错误RuntimeError:此事件循环已经在运行".

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".

这个问题在各种环境中都会出现,比如web服务器、GUI应用程序和 Jupyter 笔记本.

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

这个模块修补了 asyncio 以允许嵌套使用 asyncio.run 和loop.run_until_complete.

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

这篇关于“RuntimeError:这个事件循环已经在运行";调试 aiohttp、asyncio 和 IDE“spyder3";在 python 3.6.5 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆