Get return value from a function in thread in Python

Viewed 641

I have written a Python function that uses multithreading.

def image(link_ID):
    tid1 = Thread(target=displayImage, args=(link_ID,))
    tid2 = Thread(target=publishIAmFree)
    tid1.start()
    tid2.start()

Function displayImage() simply puts up the image and the function publishIAmFree() publishes data to the broker and returns a FLAG value.

How will I get the return value from the publishIAmFree() function, while in the thread?

1 Answers

You could try using the ThreadPool class

from multiprocessing.pool import ThreadPool
threadp = ThreadPool(processes=1)

res = threadp.apply_async(publishIAmFree, ()) # () has the arguments for function

return_val = res.get()
print return_val
Related