Python - Scheduling with stop and resume running task feature
Question: I was developing a scheduling application, in the schedule there's many lamp
that turn on and off in specific time, say if I start the schedule at morning
all lamps will turn on then stop at night, but there is some lamp that need to
stop at random time and also turn on again. I've tried some python package
like APScheduler but it doesn't have feature to stop and resume a specific
task (or lamp in this occasion).

This [question](http://stackoverflow.com/questions/6299349/how-to-stop-and-
resume-long-time-running-python-script) using pickle to stop and resume, but I
don't know how to implement it, is there any way to solve this?

Thanks in advance, sorry for my bad grammer.

**\--UPDATE--**

Here is simple implementation, I'm not sure this code is right.

    
    
    from datetime import datetime
    from time import sleep
    
    class Scheduling:
    
        def __init__(self):
            self.lamp = {}
    
        def run(self, lamp_id, start, finish):
            """Called one-time only for each lamp"""
    
            self.lamp[lamp_id] = (start, finish)
    
            while True:
                if datetime.now().strftime('%H:%M:%S') == start:
                    sleep(1)
                    print 'SET LAMP %s ON' % lamp_id
                elif datetime.now().strftime('%H:%M:%S') == finish:
                    sleep(1)
                    print 'SET LAMP %s OFF' % lamp_id
    
        def stop(self, lamp_id):
            print 'SET lamp %s OFF' % lamp_id
    
        def resume(self, lamp_id):
            print 'SET lamp %s ON' % lamp_id 
            finish = self.lamp[lamp_id][1]
    
            while True:
                if datetime.now().strftime('%H:%M:%S') == finish:
                    print 'SET lamp %s OFF' % lamp_id 
    
    if __name__ == '__main__':
        schedule = Scheduling()
        schedule.run(1, '00:00:00', '00:01:00')
    


Answer: I think you may be looking at this problem wrong. Viewing "lamp on" as a task
to be stopped and resumed is overcomplicated. Really, what you have is a
series of scheduled stateless events; turn a lamp on, turn a lamp off (and
maybe toggle lamp, that turns it on if off or off if on). If you try to model
the system that way, it will probably prove easier to set up a scheduler
around.

