[英]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.