-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python Interpreter Doesn't Work Well In Thread Class #83762
Comments
================================================================ import threading
import time
def threadFunc():
while True:
print('new thread')
time.sleep(2)
def main():
th = threading.Thread(target=threadFunc())
th.start()
while True:
print('main Thread')
time.sleep(1)
th.join()
if __name__ == '__main__':
main() ============================================================== When I run the above code in python 3.7, it works in unexpected way. I expected this code causes an syntax error for giving an improper argument to parameter because I gave "threaFunc()" not "threaFun" as an argument of target in Thread class. However, this code executes a function "threadFunc()" as a general function not thread. |
There's no other reasonable way for Python to work here. It *is* an error to pass something that's not a callable as a thread "target", but it's a coding error that can only be detected at runtime, not a syntax error that can be detected statically before the code executes. And because threadFunc() never returns, Python never gets the opportunity to check the arguments to threading.Thread: the arguments to a function call have to be evaluated before that function call can be made. It is a fairly common error, though, and it might be possible for a linter to flag this error (though probably with a smattering of false positives and false negatives). That's out of scope for Python itself. |
SyntaxError is produced when there is a syntax problem in the code to be executed, this code is fine from the syntax perspective, so this particular error would not be raised. It is also perfectly fine to have a function call where you have it, the only thing one would need to observe is that the returned value is a callable (or None which would be return value in your case had your function terminated). Perhaps there is an improvement to be made in documentation by way of giving an example, but the solution you're suggesting is not feasible. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: