簡體   English   中英

Python3 threading combining.start() 不創建 join 屬性

[英]Python3 threading combining .start() doesn't create the join attribute

這很好用:

def myfunc():
    print('inside myfunc')

t = threading.Thread(target=myfunc)
t.start()
t.join()
print('done')

然而,雖然顯然正確地創建和執行了線程:

def myfunc():
    print('inside myfunc')

t = threading.Thread(target=myfunc).start()
t.join()
print('done')

當它命中 join() 時生成以下致命錯誤:

AttributeError: 'NoneType' object 沒有屬性 'join'

我本以為這些陳述是等價的。 有什么不同嗎?

t = threading.Thread(target=myfunc).start()

threading.Thread(target = myfunc)返回一個線程對象,但object.start()返回None。 這就是存在AttributeError的原因。

自 Python 3.8 和海象運算符的引入:=你可以使用;

(t := threading.Thread(target=myfunc)).start()

暫無
暫無

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

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