Python线程模块。从Thread类实例返回函数输出?

时间:2015-11-18 21:22:21

标签: python multithreading function parallel-processing return

我尝试了以下内容,但适用于multiprocess而不是threading Python threading return values how to get the return value from a thread in python?是一个类似的问题,但答案也使用multiprocess,我想留在threading模块中。

当我创建一个Thread对象时,我不确定如何获取它所针对的函数的输出。如果我在print函数中执行return而不是choose,那么它会将其打印到控制台,但实际上我无法在更复杂的程序中使用它。

import threading
import numpy as np

def choose(seq,n=3):
    return(''.join(np.random.choice(list(seq),size=n)))

n_threads = 2
thread_list = []

alpha = "ABCD"
n = 2
print("STARTING...\n")

for i in range(n_threads):
    t = threading.Thread(target=choose,args=(alpha,n))
    t.start()
    #HOW CAN I GET VALUES FROM T? 
    thread_list.append(t)
    print("\nThread Count: " + str(threading.activeCount()))
print("\nEXITING...\n")

0 个答案:

没有答案