w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Keeping state in a constantly running python script

You need to identify the parts of your program that can be abstracted from their specific details and reused multiple times. This is an art, don't expect it to be easy. You can find many books on Object oriented programming that explain the basics and give you some direction.

Here's just a quick example:

class FileChecker(object):

    def __init__(self, path, interval):
        self.path = path
        self.interval = interval
        self.last_size = None
        self.last_mtime = None
        self.last_check_time = None

    def hasChanged(self):

    def logChange(self):

class MultiFileChecker(object):

    DELAY = 1

    def __init__(self):
        self.files = []
        self.loop_start_time = 0

    def addFile(self, f):

    def loop(self):
            print "Starting loop"
            while True:
                now = time.time()
                self.loop_start_time = now
                for f in self.files:
                    if now - f.last_check_time > f.interval:
                        if f.hasChanged():

if __name__ == '__main__':
    checker = MultiFileChecker()
    checker.add(FileChecker("/var/log/sc.log", 10))
    checker.add(FileChecker("/tmp/myfile", 20))
    checker.add(FileChecker("/home/snoppy/testfile", 30))

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.