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
A Better Way Than Looping and Calling Functions That Loop and Call Another Functions
Typeerror: Unsupported Operand Type(S) for ** or Pow(): 'List' and 'Int'
Create an Array With a Pre Determined Mean and Standard Deviation
How to Delete a Character in an Item in a List (Python)
Pyqt: Getting Widgets to Resize Automatically in a Qdialog
Check If a Python Script Is Already Running in Windows
Compare Two Lists and Find the Unique Values
Sort List Based on Another List
How to Convert SQL Query Results into a Python Dictionary
Pandas: Difference Between Pivot and Pivot_Table. Why Is Only Pivot_Table Working
Splitting Dataframe into Multiple Dataframes
Pandas - Tokenizing Data Expected 1 Field Saw Multiple
How to Download Multiple Files or an Entire Folder from Google Colab
Package Only Binary Compiled .So Files of a Python Library Compiled With Cython
How to Extract Data from Dictionary in the List
Visual Studio Code Intellisense Is Very Slow - Is There Anything I Can Do