如何在 discord.py 中使用多线程?

问题描述 投票:0回答:0

我最近尝试用 craiyon 的 API 生成一些图像,并通过 discord.py 发送图像, 但我意识到这需要很长时间,而且会阻止整个机器人一起工作。 我试图实现多线程,但我无法使用

threading.Thread()

将图像发送回类 i 实例内部

代码:

class message_del:
    def __init__(self, message):
        self._message = message
        t = threading.Thread(target=self.images)
        t.start()

    def images(self):
        if self._message.content == "$image":
            generator = Craiyon()
            result = generator.generate(self._message.content)
            result.save_images()
            self._message.reply(file=discord.File("generated//image-1.jpg"))

@client.event
async def on_message(message):
    message_del(message)

我得到这个错误:

RuntimeWarning: Enable tracemalloc to get the object allocation traceback
请帮忙

我尝试在类中添加 async 和 await,但是

__init__
没有返回 take async 然后我尝试放弃所有这些并创建图像然后通过另一个函数发送它,但是由于异步的东西,这不起作用。所以我卡住了。

python async-await discord.py python-multithreading python-class
© www.soinside.com 2019 - 2024. All rights reserved.