简体   繁体   English

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

[英]Get return value from a function in thread in Python

I have written a Python function that uses multithreading. 我已经编写了一个使用多线程的Python函数。

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. 函数displayImage()只是放置图像,而函数publishIAmFree()将数据发布到代理并返回FLAG值。

How will I get the return value from the publishIAmFree() function, while in the thread? 在线程中时,如何从publishIAmFree()函数获取返回值?

You could try using the ThreadPool class 您可以尝试使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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