How can I write code to loop in time python? I want this code loop 10 minutes in python.
msList =[]
msg = str(raw_input('Input Data :'))
msgList.append(msg)
I do not wanna use crontab
because I want this code looping in my program.
How can I write code to loop in time python? I want this code loop 10 minutes in python.
msList =[]
msg = str(raw_input('Input Data :'))
msgList.append(msg)
I do not wanna use crontab
because I want this code looping in my program.
Use the sleep
function from the time
module. This should do what you're asking:
from time import sleep
msList =[]
while True:
msg = str(raw_input('Input Data :'))
msgList.append(msg)
sleep(600)
from time import sleep
while True:
msList = []
msg = str(raw_input('Input Data :'))
msgList.append(msg)
sleep(600)
This will keep the program running and sleep for 600s or 10 minutes after it executes the first three lines inside the while
loop.
I think you are looking for something like this:
import time
t_end = time.time() + 60 * 10
msgList =[]
while time.time() < t_end:
msg = str(raw_input('Input Data :'))
msgList.append(msg)
print(msgList)
Credit goes to this post: