0

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 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')
Community
  • 1
  • 1
Kyomuu
  • 107
  • 1
  • 9

2 Answers2

1

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.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
  • I guess right, I wrote simple implementation but, I think this isn't good, when this script running my processor almost got 60% usage, just for one lamp. – Kyomuu Dec 19 '12 at 18:27
  • 1
    You probably want to use an external storage mechanism (DB, csv, yaml, etc) to record all the scheduled events, have your process keep a record of the last time it was run in another file, then process all events scheduled between the last run time and the current time from the configuration file. Then you can just schedule the OS to run the process every 15 minutes or every hour or something. – Silas Ray Dec 19 '12 at 18:31
1

Using APScheduler is not a bad solution and is what I would use in this instance. What I would do is write a customer trigger.

This trigger would use a customer database or data store, which is simple to extend from the default memory store or database store. That has a flag whether to skip or not run a particular job. So, what would happen now is when the task to turn on/off the lamp comes up the customer trigger will check the database to see if the task is turned on/off and perform the required action based on it's current state.

This can be done by looking at the Extending APScheduler Documentation.

Interval Trigger Class that would be extended to incorporate your stop/resume logic

The Interface you will need to implement for the customer datastore

EDIT:

Your implementation has one problem in that the while loops will go into an infinite loop, without a sleep in the middle, because you do not handle the else case for the inner loop part and do not sleep. This is causing your high CPU usage with a single lamp.

sean
  • 3,955
  • 21
  • 28