简体   繁体   English

Discord.py 和多线程

[英]Discord.py and multithreading

Im kinda new to programing, so this question may be dumb or easy for some... I have a question regarding multithreading and looking for some documentation or link to understand it more.我对编程有点陌生,所以这个问题对某些人来说可能很愚蠢或很容易......我有一个关于多线程的问题,正在寻找一些文档或链接以了解更多。

Lets suppose I have a Discord bot that performs a function when called.假设我有一个 Discord 机器人,它在被调用时执行一个功能。 (wrote a simple sum as an example) (写了一个简单的sum作为例子)

@client.command()
async def example(ctx):

    # (lets assume the code here is to grab the user input value
    # from two messages and transform the messages into int variables called 
    # "first" and "second")


first = int(msg1.content)
second = int(msg2.content)


# Function to perform

    def sum():
        time.sleep(5) # added a sleep to simulate the "complex" formula performing calculations and a web request.

        result = number1 + number2
        return result


    # Thread formula

    def thread():
        with ThreadPoolExecutor(max_workers = 2) as executor:
            executor.submit(sum)

    thread()
        

    
    # Send the result 
    await ctx.author.send(f'Waited 5 seconds and this is the result {thread()}')


client.run(TOKEN)

In my mind what's happening is that the thread is calling the sum function.在我看来,发生的事情是线程正在调用 sum 函数。 Of course is wrong because I get a "None" value as a result.当然是错误的,因为结果我得到了“无”值。

Can anybody point me out to some useful resources or documentation on how to implement multithreading or multiprocessing?任何人都可以向我指出一些关于如何实现多线程或多处理的有用资源或文档吗?

Or give some advice on how to handle this situation.或者就如何处理这种情况给出一些建议。 \\ \\

Is multiprocessing the solution?多处理是解决方案吗? The task is not CPU intensive, so I thought that multithreading was the way to go.任务不是 CPU 密集型的,所以我认为多线程是要走的路。

The goal is for the bot to be able to handle multiple requests at the same time.目标是让机器人能够同时处理多个请求。 Right now it does, but when the requests take a little bit more, it becomes slow or sometimes it doesn't even sends the last messages.现在确实可以,但是当请求花费更多时间时,它会变慢,或者有时甚至不发送最后一条消息。 I was testing on Repl.it and it showed a "low memory" (or something like that. And I assume its because Replit only gives like 0.4 vCPU so no big deal), which made me research about multithreading.我在 Repl.it 上进行测试,它显示“内存不足”(或类似的东西。我假设它是因为 Replit 只提供 0.4 个 vCPU,所以没什么大不了的),这让我研究了多线程。

Thanks in advance!提前致谢!

sum() is returning a value, but you're getting the answer from the wrong place. sum()正在返回一个值,但您从错误的地方得到了答案。 And by the way, you shouldn't call a function sum() , since that's already a built-in Python function.顺便说一句,你不应该调用函数sum() ,因为它已经是一个内置的 Python 函数。 But that's another matter.但那是另一回事。

executor.submit() returns a Future `. executor.submit() returns a Future `。 That future will get the result when the sum() has finished当 sum() 完成时,future 将得到结果

You want to write:你想写:

    def thread():
        with ThreadPoolExecutor(max_workers = 2) as executor:
            return executor.submit(sum)

    future = thread()
    return future.result()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM