我尝试了以下内容,但适用于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")