2022-04-27

Python in javascript (Brython) execute function with timeout

I'm trying to execute a python function with a timeout, I've found some good ideas on stackoverflow but they don't seem to work for me as I'm executing the python function from javascript (using Brython) and multithreading/sleep don't work well (no sleep function in javascript). Any idea relatively easy to implement that would allow me to terminate a function if its execution takes more than 10s (see logic below):

def function_to_execute:
    print("function executing")

time_out=10
exec(function_to_execute)
time_function_started=time()
if time()>(time_function_startedtime_out) and function_to_execute not complete: (simplified for clarity)
    function_to_execute.terminate()

Thanks,



No comments:

Post a Comment