0

There's lots of questions about it but I don't quite get it yet.
I have to upload some code into a server to run 24/7, and this code will be basically something like this:

while True:
    variable1 = function1()
    variable2 = function2()
    variable3 = function3()

So let's say I want to do variable2 = function2() once every hour or so, for performance (I don't understand much about it, but I think it'll do good to put a timer on it)

I can't use time.sleep() on it because the code has to keep going
I mentioned the server thing because I don't know if answers using a system timer works with it, I'm still learning how to do it.
Forgot to say I'm using Python 2.7

Tax
  • 49
  • 1
  • 9
  • Are you asking about threading? – whackamadoodle3000 Aug 20 '17 at 23:35
  • @whackamadoodle3000 I don't know what it is, let me check – Tax Aug 20 '17 at 23:36
  • do you want what time take to each function end? – keyvan vafaee Aug 20 '17 at 23:37
  • @keyvanvafaee I'm not sure I understand... Function 2 isn't necessary to run like 100 times per hour, I just need it one time. But the while has to keep going and all the code is in it, so I'd need to put a timer just for that single call. – Tax Aug 20 '17 at 23:38
  • If you want 3 periodic actions to be performed with *different* frequencies, consider running them as completely separate processes, scheduled by the `cron` utility (you'll find that outside the Python universe, but it can run Python scripts for you just like it can run anything else). If you must have these functions in the same process, then it sounds like you need the three functions to run in separate *threads*. Python can handle that for you via the `threading` module. But threading is a deep-ish topic with a few gotchas lying in wait for the unwary, so get googling. – jez Aug 20 '17 at 23:38
  • @jez So, import threading and variable2 = threading.Timer(3600, function2) should keep the while going but only executing function2 once every hour, is that correct? – Tax Aug 20 '17 at 23:45
  • @Tax That's one way to do it, for sure. But note that `variable2` is now not the output of `function2()`: rather it's the timer object (which, incidentally, also has to be `.start()`ed). Your comment to keyvanvafee above made me think you might be able to get away with something simpler, hence my (completely non-threaded) answer below. Really the pros and cons of each approach are going to come down to the details of what further actions you need to perform with the outputs `variable1`, `variable2`, etc. – jez Aug 20 '17 at 23:51
  • Correction: actually I think `Timer` only runs the function *once*, after the specified delay. Not repeatedly. Another approach is to start a `threading.Thread` object, whose `target` is a function that contains `function2()` calls in their own dedicated `while` loop. – jez Aug 20 '17 at 23:55
  • @COLDSPEED mark-as-duplicate is wrong: the OP is not asking about timeouts. – jez Aug 21 '17 at 00:05

1 Answers1

1

Not sure if this is what you want, but here is a single loop that runs three functions with three different periodicities:

import time

t1 = t2 = t3 = 0

period1 = 1.0 # do function1() every second
period2 = 3600.0  # do function2() every hour
period3 = 60.0 # do function3() every minute

sleep_seconds = 0.1   # or whatever makes sense

while True:

    t = time.time()

    if t - t1 >= period1:
        variable1 = function1()
        t1 = time.time()

    if t - t2 >= period2:
        variable2 = function2()
        t2 = time.time()

    if t - t3 >= period3:
        variable3 = function3()
        t3 = time.time()


    time.sleep( sleep_seconds )
jez
  • 14,867
  • 5
  • 37
  • 64
  • That could possibly work. So t increases with the system time, is it? Would that have a problem if it was run from a server (I really don't know how a server works) – Tax Aug 20 '17 at 23:54
  • `time.time()` returns the number of seconds elapsed since the champagne corks popped in London and Johannesburg for New Year 1970. The only circumstance in which I can foresee a problem is if the server's clock is set significantly wrong for some odd reason (like, hours ahead of where it should be) and it corrects itself via a time server while you're running. – jez Aug 21 '17 at 00:02
  • It should work fine then, it's my poor attempt at saving some requests only. Thank you! – Tax Aug 21 '17 at 00:06