How do I limit the running time of a script? For example, I set the program to close after 5 minutes, it works and as soon as 5 minutes pass the program will close. How can this be done? In windows.
The script code is executed in the
join waits for the specified amount of time, blocking further code execution on the main thread. After the specified time has elapsed, the script will print a message to the console and close.
daemon=True parameter specifies that the thread will also terminate when the main thread terminates:
import time import threading def run(): i = 1 # Бесконечный цикл while True: print i i += 1 time.sleep(1) if __name__ == '__main__': thread = threading.Thread(target=run) thread.daemon = True thread.start() thread.join(5 * 60) print 'Quit!'