簡體   English   中英

線程 Python3

[英]Threading Python3

我正在嘗試在 Python 中使用線程,並努力同時啟動兩個函數,然后等待兩者完成並將返回的數據加載到主代碼中的變量中。 如何做到這一點?

import threading
from threading import Thread

func1():
 #<do something>
 return(x,y,z)

func2():
 #<do something>
 return(a,b,c)

Thread(target=func1).start()
Thread(target=func2).start()
#<hold until both threads are done, load returned values>

從提出的問題中肯定需要更清楚。 也許您正在追求以下內容?

import threading
from threading import Thread

def func1():
 print("inside func1")
 return 5

def func2():
    print("inside func2")
    return 6


if __name__ == "__main__":
    t1 = Thread(target=func1)
    t2 = Thread(target=func2)
    threads = [t1, t2]
    for t in threads:
        t.start()

我相信您錯過了實際啟動線程的 start() 方法?

暫無
暫無

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

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