简体   繁体   中英

Get return value from a function in thread in Python

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM