While developing my code, I encountered an issue where the webpage becomes unresponsive and irrecoverable when an infinite loop is executed in async mode. While searching for a solution, I found a related discussion here. I tried applying the suggestions to my code, but it didn’t work as expected.
Here’s my code:
from browser import document, window, aio, console, timer
# Set loop timeout
timer.set_loop_timeout(10)
...
def processInput(event=None):
global input_values
# Clear previous output
document["mainConsole"].innerHTML = ""
# Execute the script
aio.run(exec_script())
async def exec_script():
try:
${solutiontext} # This may include an infinite loop
except BrythonNotRunningError:
on_compile_done()
return
except Exception as e:
print(f"Error: {e}")
finally:
on_compile_done()
processInput()
The problem is that when the solutiontext
variable contains an infinite loop like while(1):
, the entire webpage freezes. It seems that the set_loop_timeout(10)
option doesn’t work within the asynchronous function exec_script()
.
Has anyone encountered this issue before? How can I prevent or handle infinite loops in asynchronous Brython code?