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

9
  • Are you asking about threading? Commented Aug 20, 2017 at 23:35
  • @whackamadoodle3000 I don't know what it is, let me check Commented Aug 20, 2017 at 23:36
  • do you want what time take to each function end? Commented Aug 20, 2017 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. Commented Aug 20, 2017 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. Commented Aug 20, 2017 at 23:38

1 Answer 1

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 )
Sign up to request clarification or add additional context in comments.

3 Comments

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)
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.
It should work fine then, it's my poor attempt at saving some requests only. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.