在异步事件循环中运行.render()(来自requests_html)时,出现错误“此事件循环已在运行”

时间:2018-09-04 13:39:04

标签: python python-3.x python-requests python-asyncio

我正在使用discord.py构建一个Discord机器人,该机器人可以从ESPN获得实况足球比分。到目前为止,我有:

Bot.py:

import discord, asyncio
from Scores import GetScores

client = discord.Client()

@client.event
async def on_message(message):
    if message.content.startswith("!scores"):
        Output = GetScores(message.content)

# rest of code

Scores.py:

from requests_html import HTMLSession

def GetScores(Message):
    Link = "http://www.espn.co.uk/football/scoreboard"

    Session = HTMLSession()
    Response = Session.get(Link)
    Response.html.render()

# rest of code  

因此,在Discord中发送“!scores”命令时,Bot.py将运行事件循环并从Scores.py调用“ GetScores”函数。

问题是,运行Response.html.render()时,出现“此事件循环已在运行”错误。从那时开始的完全错误:

    Response.html.render()
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests_html.py", line 572, in render
    self.session.browser  # Automatycally create a event loop and browser
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\requests_html.py", line 680, in browser
    self._browser = self.loop.run_until_complete(pyppeteer.launch(headless=True, args=['--no-sandbox']))
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running  

this GitHub issue中,我发现该代码并非旨在在现有事件循环中运行。但是,我想知道asyncio中是否有变通办法,以允许它在这种情况下运行。我更希望找到一个解决方法,而不是另一个解决方案/模块,因为在Discord事件循环中测试此方法之前,我先使用此方法编写了整个文档,然后发现它不起作用。

任何帮助将不胜感激,谢谢!

0 个答案:

没有答案