简体   繁体   English

Spyder:无法关闭正在运行的事件循环

[英]Spyder: Cannot close a running event loop

I am currently checking out the Python discord wrapper found here but it doesn't seem to work due to the above mentioned error.我目前正在检查此处找到的 Python discord 包装器但由于上述错误,它似乎不起作用。 I tried calling the nest_asyncio.apply() before running the client.run() function but it doesn't seem to work either as mentioned in this other question .我尝试在运行client.run()函数之前调用nest_asyncio.apply()但它似乎也不起作用,如另一个问题中所述 This question is possibly a duplicate but couldn't add a comment in the previous one.这个问题可能是重复的,但无法在上一个中添加评论。

I tried this:我试过这个:

nest_asyncio.apply()
client = discord.Client()
client.run(bot_token)

as well as this to no avail:以及这无济于事:

client = discord.Client()
nest_asyncio.apply(client.run(bot_token))

I've faced a similar, if not the same issue a few months ago, and what I found on a comment to a github issue ultimately led to the following:几个月前我遇到了一个类似的问题,如果不是同样的问题,我在对github 问题评论中发现的内容最终导致了以下问题:


class DiscordAccessor:
    '''class to handle discord authentication and async loop handling
    Attributes
    ----------
        dc_coroutine
            the coroutine to start the client
        dc_thread
            the thread to keep the coroutine alive
    Methods
    -------
        start_loop
            starts the async loop'''
    dc_loop = asyncio.new_event_loop()
    client = discord.Client(loop=dc_loop)

    def __init__(self):
        self.dc_coroutine = DiscordAccessor.client.start('YOUR_TOKEN')
        self.dc_thread = threading.Thread(target=self.start_loop,
                                args=(self.dc_loop, self.dc_coroutine))
        self.dc_thread.start()

    def start_loop(self, loop, coro):
        '''starts the async loop
        Parameters
        ----------
            loop
                the asyncio loop
            coro
                the coroutine'''
        loop.run_until_complete(coro)

This class wraps the discord client into it's own thread and event loop.这个类将 discord 客户端包装到它自己的线程和事件循环中。 You'd call your client something like:你会这样称呼你的客户:

dc = DiscordAccessor()
dc.client.whatever_you_want_to_call()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法关闭正在运行的事件循环 - Cannot close a running event loop Python:“无法关闭正在运行的事件循环”Asyncio - Python: "Cannot close a running event loop" Asyncio RuntimeError:无法关闭正在运行的事件循环 - RuntimeError: Cannot close a running event loop 无法关闭正在运行的事件循环,当我调用 run_forever 时我无法关闭循环 - Cannot close a running event loop,when i call run_forever i can't close the loop (Python) Discord bot 代码返回“RuntimeError:无法关闭正在运行的事件循环” - (Python) Discord bot code returns "RuntimeError: Cannot close a running event loop" 如何修复运行时错误:无法关闭正在运行的事件循环 - Python Discord Bot - How to Fix Runtime Error: Cannot close a running event loop - Python Discord Bot “运行时错误:此事件循环已在运行”; 在 python 3.6.5 中调试 aiohttp、asyncio 和 IDE“spyder3” - “RuntimeError: This event loop is already running”; debugging aiohttp, asyncio and IDE “spyder3” in python 3.6.5 '在另一个循环正在运行时无法运行事件循环') RuntimeError websockets? - 'Cannot run the event loop while another loop is running') RuntimeError websockets? RuntimeError:无法从正在运行的事件循环中调用 asyncio.run() - RuntimeError: asyncio.run() cannot be called from a running event loop 事件循环已经在运行 - The event loop is already running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM