0

Let's say I have built a small custom class - I think quickly built a representative prototype below - to help me hold on to and operate on some data.

The ultimate use case, though, is to hold on to data and periodically delete older data, really just holding onto newer data. I was thinking to start I would just try to delete all of the data after 15 seconds. And understand that in trying to do things/operate on the data I have, well, that it's all less than 15 seconds old for sure. So it's all new data.

And then later I could make the class more complicated - do things like - not delete all of the data but just the VERY old stuff, or change the 15 second interval to something dynamic/smarter, etc.

But before I even got near that point, I was stumbled by the way threading/timer worked. I discovered empirically that the below code only prints out 'Fresh!' if I include that second/subsequent threading.Timer call INSIDE the freshen_up method which is the callback. I would have thought I'd only need to start the timer once and it would just call the callback every 15 seconds. Am I crazy, or do I need to set the timer 'every time' (as I do in the code below), for this simple implementation to work? Again, as far as I can tell, this code works, I am just surprised that I needed to start a new timer every time. So I want to make sure I am not doing something inefficient/unnecessary due to my lack of understanding how timer works... thanks

import threading

class Fresh_Data_Container():

    def __init__(self):

        self.fresh_data_dict = {}
        threading.Timer(15.0, self.freshen_up).start()

    def freshen_up(self):

        self.fresh_data_dict = {} # starting over for now, perhaps more nuanced later 
        print ('Fresh!')

        threading.Timer(15.0, self.freshen_up).start() # does this really have to be here or am i looking at this wrong?

    def add_fresh_data(self, some_key, fresh_data):
        if some_key in self.fresh_data_dict:
            self.fresh_data_dict[some_key].append(fresh_data)
        else:
            self.fresh_data_dict[some_key] = [fresh_data]

    def operate_on_data(self):
        pass # do stuff 

1 Answer 1

2

threading.Timer isn't periodic -- that is, it only calls the given function once after the elapsed 15 seconds, not every 15 seconds.

A better way to do this would be to create a regular Thread instead of a Timer:

def __init__(self):
    self.fresh_data_dict = {}
    self.running = True

    threading.Thread(target=self.freshen_up, daemon=True).start()

def stop(self):
    self.running = False

def freshen_up(self):
    while self.running:
        time.sleep(15)
        self.fresh_data_dict.clear()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.