Search code examples
pythonpython-3.xpython-multithreading

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?


Solution

  • 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.