Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This works fine:

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

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

However this, while apparently creating and executing the thread properly:

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

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

Generates the following fatal error when it hits join():

AttributeError: 'NoneType' object has no attribute 'join'

I would have thought that these statements are equivalent. What is different?

like image 962
FarNorth Avatar asked Nov 15 '25 01:11

FarNorth


1 Answers

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

threading.Thread(target=myfunc) returns a thread object, However object.start() returns None. That's why there is an AttributeError.

like image 195
Himanshu Gupta Avatar answered Nov 17 '25 14:11

Himanshu Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!