【问题标题】:Spyder: Cannot close a running event loopSpyder:无法关闭正在运行的事件循环
【发布时间】:2020-06-23 02:48:49
【问题描述】:

我目前正在检查 Python discord 包装器found here,但由于上述错误,它似乎不起作用。我尝试在运行client.run() 函数之前调用nest_asyncio.apply(),但它似乎也没有像其他question 中提到的那样工作。这个问题可能是重复的,但无法在上一个问题中添加评论。

我试过这个:

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

还有这无济于事:

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

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    几个月前我遇到过类似的问题,如果不是同样的问题,我在comment to a github issue 上发现的内容最终导致了以下问题:

    
    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)
    

    这个类将不和谐客户端包装到它自己的线程和事件循环中。你会这样称呼你的客户:

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

    【讨论】:

    • 在进行了一些更改以使其适用于 Python 3(线程模块已被弃用)后,我仍然在 Spyder 上遇到同样的错误。
    • 你运行的是什么 Python 版本和 discord.py-version?我一直在使用 Python 3.6 和 discord.py 1.2.5
    • Python 3.7.4 和 discord.py 1.3.2
    • 我可能应该指出我一直在使用threading.Thread,忘记添加我的导入。也就是说,除了我提到的设置之外,我从未使用任何其他设置测试过此解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-30
    • 2021-11-17
    • 2020-08-02
    • 1970-01-01
    • 2019-12-29
    • 2021-01-25
    相关资源
    最近更新 更多