简体   繁体   English

python 线程 - 多参数和返回

[英]python threading - multiple parameter and return

I have a method like this in Python:我在 Python 中有这样的方法:

def test(a,b):
    return a+b, a-b

How can I run this in a background thread and wait until the function returns.如何在后台线程中运行它并等待 function 返回。

The problem is the method is pretty big and the project involves GUI, so I can't wait until it's return.问题是方法很大,项目涉及GUI,所以我不能等到它返回。

In my opinion, you should besides this thread run another thread that checks if there is result.在我看来,除了这个线程之外,你应该运行另一个线程来检查是否有结果。 Or Implement callback that is called at the end of the thread.或者实现在线程结束时调用的回调。 However, since you have gui, which as far as I know is simply a class -> you can store result into obj/class variable and check if the result came.但是,由于您有 gui,据我所知,它只是一个 class -> 您可以将结果存储到 obj/class 变量中并检查结果是否出现。

I would use mutable variable, which is sometimes used.我会使用有时使用的可变变量。 Lets create special class which will be used for storing results from thread functions.让我们创建特殊的 class 用于存储线程函数的结果。

import threading
import time
class ResultContainer:
    results = [] # Mutable - anything inside this list will be accesable anywher in your program

# Lets use decorator with argument
# This way it wont break your function
def save_result(cls):
    def decorator(func):
        def wrapper(*args,**kwargs):
            # get result from the function
            func_result = func(*args,**kwargs)

            # Pass the result into mutable list in our ResultContainer class
            cls.results.append(func_result)

            # Return result from the function
            return func_result

        return wrapper

    return decorator

# as argument to decorator, add the class with mutable list
@save_result(ResultContainer)
def func(a,b):
    time.sleep(3)
    return a,b


th = threading.Thread(target=func,args=(1,2))
th.daemon = True
th.start()

while not ResultContainer.results:
    time.sleep(1)
print(ResultContainer.results)

So, in this code, we have class ResultContainer with list .所以,在这段代码中,我们有 class ResultContainerlist Whatever you put in it, you can easily access it from anywhere in the code (between threads and etc... exception is between processes due to GIL).无论您放入什么,您都可以从代码中的任何位置轻松访问它(线程之间等......由于 GIL,进程之间出现异常)。 I made decorator , so you can store result from any function without violating the function.我制作了装饰器,因此您可以存储任何 function 的结果,而不会违反 function。 This is just example how you can run threads and leave it to store result itself without you taking care of it.这只是一个示例,您可以如何运行线程并让它自己存储结果而无需您处理它。 All you have to do, is to check, if the result arrived.您所要做的就是检查结果是否到达。

You can use global variables, to do the same thing.您可以使用全局变量来做同样的事情。 But I dont advise you to use them.但我不建议你使用它们。 They are ugly and you have to be very careful when using them.它们很丑陋,使用它们时必须非常小心。


For even more simplicity, if you dont mind violating your function, you can just, without using decorator, just push result to class with list directly in the function, like this:为了更简单,如果您不介意违反 function,您可以在不使用装饰器的情况下将结果推送到 class,并直接在 ZC1C425268E68385D1AB5074C17A94F1 中列出列表,如下所示:

def func(a,b):
    time.sleep(3)
    ResultContainer.results.append(tuple(a,b))
    return a,b

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

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