从Python中的线程函数获取返回值

我编写了一个使用多线程的
Python函数.

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

函数displayImage()只是放置图像,函数publishIAmFree()将数据发布到代理并返回FLAG值.

在线程中,如何从publishIAmFree()函数获取返回值?

最佳答案 您可以尝试使用ThreadPool类

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
点赞