-
Notifications
You must be signed in to change notification settings - Fork 36
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
How to stop timeout task #10
Comments
you should try this code for same: Code :
Output :
|
@Utsav13 Thanks for relay, but the function import time
from func_timeout import func_set_timeout , FunctionTimedOut
@func_set_timeout(3)
def test():
while True:
time.sleep(1)
print("test")
try:
test()
except FunctionTimedOut:
print("end test!!")
print("function ended syccesfully!!!")
for i in range(1,10):
time.sleep(1) Output:
|
okay, previously I tested code with python 3.6 and it works fine. but when I have tested it with python 2.7 then I got the same error which u have right now. so if it is possible to use the same code with python 3.x then you have not to change anything with your code. but if your python version is mandatory for your use then you can use simple tricks to exit the loop.
|
You have found a real bug! This works in a file in python2, and works in python3 both interactive and with a file. Here is my output for running the same in a file:
And the COMPLETE output:
func_timeout.exceptions.FunctionTimedOut: Function test (args=()) (kwargs={}) timed out after 3.000000 seconds. Caveat I AM able to reproduce this by running the exact same code in the python2 interactive terminal, however it does NOT reproduce in a standalone file. This appears to be a bug in python, see the output of the threading.enumerate() call, which lists all active threads:
Show that it is not alive:
So.... there is obviously some bug in python2 here, where it is still running a thread that it has internally marked as dead, and is not cleaning it up. Trying the same in python3 interactive terminal works as expected:
func_timeout.exceptions.FunctionTimedOut: Function test (args=()) (kwargs={}) timed out after 3.000000 seconds. And as you can see, the thread is properly cleaned-up.
I tested the following patch, which seems to stop the thread after 6 seconds (instead of the expected 3). Note in this case I reduced "repeatEvery" / "raiseEvery" from 2.0 seconds to .25 seconds for brevity sake.
And you can see the results: Test
So, even though it is marked dead, apparently hammering it with exceptions and joins finally works around the bug and makes it die. I will have to find a clean way to accomplish this for python2 (which is just about EOL, btw...), as the above patch obviously isn't acceptable. I am not sure if this is limited to the print function, or if other things (such as math) retain the running. Stay tuned! |
`@func_set_timeout(7) try: myloop(10) print ('************') try: func_timeout(5, myloop, args=[10]) Output: 0 6 I use python 3.7 and this issue is still there. I've embedded the myloop function within the func_set_timeout decorator with 7 seconds timeout. |
This is still an issue with Python 3.6, which sadly renders the library pretty much useless :( |
on python 3.10 the bug mentioned is fixed, however import time
import func_timeout
from func_timeout import func_set_timeout
@func_set_timeout(1)
def jls_extract_def():
return print(eval(s))
s='4.10**15**10**9'
try:
jls_extract_def()
except func_timeout.exceptions.FunctionTimedOut:
print('out_time')#
# return 1 eval can not be interupted correctly |
I am trying to figure out the correct way to use this library, I use the following test code:
After the
func_timeout.exceptions.FunctionTimedOut
, thetest
function is not terminated, how can i stop the function?The text was updated successfully, but these errors were encountered: