0

How can I print "hello world" every 34 minutes in python? Right now I'm grabbing the system time, comparing it to my last recording time, looping around (which is a pain for 24 hour clocks), rinse and repeat. Is there a nice and easy way to do this.

To put simply, how would you loop code this code every 34 minutes:

print("Task to do every 34 minutes")

Monte Carlo
  • 447
  • 2
  • 6
  • 14
  • 1
    Using `time.sleep` as noted in the answers is probably the option you want, but if for some reason you need to actually keep track of the time (for logging, for example), you can use `datetime.datetime` and `datetime.timedelta` so you don't have to worry about the 24 hour clock question. – Silas Ray Sep 10 '13 at 18:01

2 Answers2

6

Use the time module:

from time import sleep

while True:
    print("Task to do every 34 minutes")

    sleep(60 * 34)  # argument is seconds
Marco Scannadinari
  • 1,774
  • 2
  • 15
  • 24
1

This can be a solution:

import time

while True:
    print "hello world"
    time.sleep(2040)

Sleep takes argument as number of seconds so 60*34 = 2040.

Jonathan Hartley
  • 15,462
  • 9
  • 79
  • 80
jandresrodriguez
  • 794
  • 7
  • 18