Ask Your Question
0

How can the running loop in Python Eel be stopped after several minutes?

asked 2023-06-18 10:18:10 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-18 10:34:02 +0000

bukephalos gravatar image

You can implement a timer inside the Python code that will stop the running loop after several minutes. Here's an example code snippet that runs a loop for 5 minutes and then stops:

import time
import sys
import eel

# Define the function to be called from JavaScript
@eel.expose
def my_function():
    # Start a timer for 5 minutes
    start_time = time.time()
    end_time = start_time + 300 # 300 seconds = 5 minutes
    while time.time() < end_time:
        # Your loop code goes here
        # ...
        # Check if time is up
        if time.time() >= end_time:
            break
    # End the Eel application
    eel._sleep(1) # Wait for any running JavaScript to finish
    eel._stop() # Stop the Eel application and exit Python

# Start the Eel application
eel.init('web')
eel.start('index.html', size=(1000, 600))

In this code, we use the time module to start a timer for 5 minutes (end_time = start_time + 300). Inside the loop, we run our code and then check if the time is up (if time.time() >= end_time:). If it is, we break out of the loop and stop the Eel application (eel._stop()). Note that we use eel._sleep(1) to wait for any running JavaScript to finish before stopping the application.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-18 10:18:10 +0000

Seen: 31 times

Last updated: Jun 18 '23