I tried the following but it is for multiprocess
not threading
Python threading return values
how to get the return value from a thread in python? is a similar question but the answer also uses multiprocess
and I would like to stay in the threading
module.
When I create a Thread
object, I'm not sure how to get the output of the function that it is targeting. If I do print
instead of return
in my choose
function, then it will print it out to the console but I can't actually use that in a more complicated program.
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")