0

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.

Phani
  • 5,319
  • 6
  • 35
  • 43
Imam Abdul Mahmudi
  • 293
  • 2
  • 5
  • 16

3 Answers3

0

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)
rofls
  • 4,993
  • 3
  • 27
  • 37
0
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.

Leb
  • 15,483
  • 10
  • 56
  • 75
  • thank you for the answer, but I want in 10 minutes I can run code for input. – Imam Abdul Mahmudi Sep 29 '15 at 01:37
  • 1
    It is asking for input after 10 minutes, set it to `sleep(5)` and test it. After 5 seconds it'll ask for input and wait until you give it something. – Leb Sep 29 '15 at 01:42
0

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:

Python loop to run for certain amount of seconds

Community
  • 1
  • 1
idjaw
  • 25,487
  • 7
  • 64
  • 83