timeout-decorator icon indicating copy to clipboard operation
timeout-decorator copied to clipboard

Unexpected behaviour when using timeout

Open raghunadhNarne opened this issue 1 year ago • 0 comments

Hi, Here in python_runner.py file i am reading a python file(myscript.py) and running the script with timeout.

python_runner.py:

import timeout_decorator
try:
        @timeout_decorator.timeout(5)
        def execute_script_file_with_timeout():
            exec(open(myscript.py).read())
        execute_script_file_with_timeout()
    except timeout_decorator.timeout_decorator.TimeoutError:
        print("Task Execution Timeout")

when i use this script in myscript.py, this is working as expected.

import random
import string
print("\n".join("".join(random.choices(string.ascii_uppercase, k=1024))))

when i use this script in myscript.py,

import random
import string
print("\n".join("".join(random.choices(string.ascii_uppercase, k=1024)) for _ in range(1024)))

i am getting below error,

Traceback (most recent call last): File "python_runner.py", line 56, in execute_script_file_with_timeout() File "/home/user1/.local/lib/python3.8/site-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function return function(*args, **kwargs) File "python_runner.py", line 55, in execute_script_file_with_timeout exec(open(myscript.py).read()) File "", line 7, in File "", line 7, in NameError: name 'random' is not defined

Thanks, Raghu

raghunadhNarne avatar Mar 29 '24 08:03 raghunadhNarne