How to Keep Python Script Keep Running Indefinitely

How to run a script forever?

Yes, you can use a while True: loop that never breaks to run Python code continually.

However, you will need to put the code you want to run continually inside the loop:

#!/usr/bin/python

while True:
# some python code that I want
# to keep on running

Also, time.sleep is used to suspend the operation of a script for a period of time. So, since you want yours to run continually, I don't see why you would use it.

How do I keep my python backend scripts running forever on Microsoft Windows?

You could use https://docs.microsoft.com/en-US/windows-server/administration/windows-commands/sc-create to create a service then use Scheduled Tasks to control it.

How to automatically keep a script running?

The following will run your program forever until you force it to quit (using Ctrl-C):

while True:
num = int(input("Enter First Number: "))
print(num / 1440)

Or as 0x5453 mentioned, you can use a "poison pill". An input given by the user to kill the loop.

while True:
response = input("Enter First Number: ")
if response == ":q":
break
print(int(response) / 1440)

If the user types :q to the program, it will break out of the forever loop and end your program.

How to run an infinite loop while continuing the rest of the script in Python

I suggest you use multithreading concept. By using multithreading what you can do is have this infinite loop running in your separate thread and the rest of the code will keep running without interruption.

Also if you want to share some variables in this thread and your script, don't forget to use global for variable.

Some reference link : https://www.geeksforgeeks.org/multithreading-python-set-1/



Related Topics



Leave a reply



Submit