user1688175
user1688175

Reputation:

How to better work with time related commands in Python?

Is there a better way to deal with a problem like this, avoiding some many useless comparisons?

import time

logClock = time.time()

while True:
    #Do something every 15 minutes
    if (time.time() - logClock) > 900:
        logClock = time.time()
        #Do something

Thank you!

Upvotes: 0

Views: 67

Answers (2)

user1688175
user1688175

Reputation:

I decided to use a timer:

def wlanCheck():
    #Do something..

    #Timer controller...
    if util.getThreadKill() != 1:
        global tWLAN
        tWLAN = 0
        tWLAN = Timer(900, wlan.WLAN_check)
        tWLAN.start()

tWLAN = Timer(900, wlan.WLAN_check)

Upvotes: 0

Or Duan
Or Duan

Reputation: 13860

Hey while you can use an infinity loop with sleep, I would recommend to use Cron.

Since your goal is to run a script every period of time, with Cron you can schedule scripts like that.

If I'm not worng this is what you need in you Cron jobs file:

*/15 * * * * /usr/bin/python /path/to/script.py

When using Python you can do that with code-only via python-crontab lib.

Goodluck.

Upvotes: 1

Related Questions