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 shold start running and go on to the next line, shouldn’t it?)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Because of the trailing () on the target=self.read(), you’re running self.read in the wrong thread, the current thread — not the new thread you’re creating — and passing the return value of the self.read call as the target argument of Thread. Thread expects to be passed a function to call, so just remove the parentheses and remember to start the thread:
t1=threading.Thread(target=self.read)
t1.start()
print("something")
For targets that need arguments, you can use the args and kwargs arguments to threading.Thread, or you can use a lambda. For example, to run f(a, b, x=c) in a thread, you could use
thread = threading.Thread(target=f, args=(a, b), kwargs={'x': c})
or
thread = threading.Thread(target=lambda: f(a, b, x=c))
though watch out if you pick the lambda – the lambda will look up f, a, b, and c at time of use, not when the lambda is 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 call start immediately).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0