t1=threading.Thread(target=self.read())
print("something")
t2=threading.Thread(target=self.runChecks(), args=(self,))
self.read runs indefinitely, so the program won’t ever reach the print line. How is this possible without calling t1.start()? (Even if I call that, it should start running and go on to the next line, shouldn’t it?)
See also: What does it mean when the parentheses are omitted from a function call (supposing no arguments are necessary)? for a deeper understanding of the bug, and Python Argument Binders for a more general solution.
Because of the trailing
()on thetarget=self.read(), you’re runningself.readin the wrong thread, the current thread — not the new thread you’re creating — and passing the return value of theself.readcall as thetargetargument ofThread.Threadexpects to be passed a function to call, so just remove the parentheses and remember to start the thread:For targets that need arguments, you can use the
argsandkwargsarguments tothreading.Thread, or you can use a lambda. For example, to runf(a, b, x=c)in a thread, you could useor
though watch out if you pick the lambda – the lambda will look up
f,a,b, andcat time of use, not when thelambdais defined, so you may get unexpected results if you reassign any of those variables before the thread is scheduled (which could take arbitrarily long, even if you callstartimmediately).