Blake Rivell
Blake Rivell

Reputation: 13875

Python script making multiple web requests to update values using threading.Timer

I am currently writing a python script that needs to stay running on a linux server. The main function of the script called calculateValues() is responsible for running every 30 seconds and using the latest values of value1, value2, value3 in its actions such as sending notifications based on thresholds.

Each one of the values will have a different update frequency. Notice how one of them needs to make an api request and be updated every 100 seconds, another every 5000 seconds, and another every 84000 seconds. Regardless of when these values update the calculateValues function will be running every 30 seconds and using whatever the current value1, value2, value3 values are at that time in its work.

A few questions I have:

  1. Am I going about this the correct way using threading.Timer? I noticed there is also a sched feature that seemed pretty cool, but didn't see the point in using it since it looked like it is mostly used for delaying a certain amount of time and then running a function.

  2. In order to run this script on a linux box 24/7 would the best way just be to move the code to it and run the main python file in terminal and just leave it open? Or should I be packaging this up as an actual python application?

  3. Any recommendations on how this code is structured/written would be helpful as well. Something seems off about this global variable approach I am taking.

Below is a sample of the script:

#!/usr/bin/env python3

import threading

value1 = None
value2 = None
value3 = None

def calculateValues():
    threading.Timer(30, calculateValues).start()
    print('Value 1: ' + str(value1))
    print('Value 2: ' + str(value2))
    print('Value 3: ' + str(value3))

    # Do something with these values here every 30 seconds

def updateValue1():
    threading.Timer(100, updateValue1).start()
    # make request to web api to get latest value1
    valueFromRequest = 1000
    # update global var value1
    global value1
    value1 = valueFromRequest

def updateValue2():
    threading.Timer(5000, updateValue2).start()
    # make request to web api to get latest value2
    valueFromRequest = 1000
    # update global var value2
    global value2
    value2 = valueFromRequest

def updateValue3():
    threading.Timer(84000, updateValue3).start()
    # make request to web api to get latest value3
    valueFromRequest = 1000
    # update global var value3
    global value3
    value3 = valueFromRequest

updateValue1()
updateValue2()
updateValue3()
calculateValues()

Upvotes: 0

Views: 48

Answers (1)

Yogaraj
Yogaraj

Reputation: 320

Am I going about this the correct way using threading.Timer? I noticed there is also a sched feature that seemed pretty cool, but didn't see the point in using it since it looked like it is mostly used for delaying a certain amount of time and then running a function.

This SO answer seems to discuss about the difference and need for sched and threading.Timer

In order to run this script on a linux box 24/7 would the best way just be to move the code to it and run the main python file in terminal and just leave it open? Or should I be packaging this up as an actual python application?

If you're using threading.Timer just for scheduling the next job, the best would be adding a cron job in the linux box and let the cron handle the work.

Any recommendations on how this code is structured/written would be helpful as well. Something seems off about this global variable approach I am taking.

If you're going with the cron job way of executing, it would be best to pass some arguments to the file and execute the required method and there wouldn't be a need for Timer itself.

Upvotes: 1

Related Questions