簡體   English   中英

從Python中的線程中的函數獲取返回值

[英]Get return value from a function in thread in 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM