0

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")
Community
  • 1
  • 1
O.rka
  • 29,847
  • 68
  • 194
  • 309
  • Please be aware that the `threading` and `multiprocessing` modules have _very_ similar functions and classes. I guess that that is why your answer was marked as being a duplicate. But the suggested dup. is likely [not the only one](http://stackoverflow.com/questions/1886090/return-value-from-thread) – cfi Nov 18 '15 at 21:50
  • @cfi thanks, the last answer on http://stackoverflow.com/questions/1886090/return-value-from-thread was what I was looking for. – O.rka Nov 18 '15 at 22:04

0 Answers0