Force sleep to ignore interrupts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andychambers2002@yahoo.co.uk

    #1

    Force sleep to ignore interrupts

    I've written a simple Timer class that allows you to extend it
    and then implement onMinuteChange, onHourChange etc methods
    which will be executed on each new minute/hour respectively.

    It works as I want when used in the main application thread.
    That is, when you hit Ctr + C, it stops running. However, if
    the class that subclasses it, also subclasses Thread, it breaks
    in that hitting Ctrl + C interrupts the call to sleep which puts
    the event loop out of sync with real time.

    How can I force the sleep to stay asleep for a whole second? The
    source code is below.


    from threading import Thread
    from datetime import datetime
    from time import sleep

    new_minute = lambda t: t.second == 0
    new_hour = lambda t: t.minute == 0 and new_minute(t)
    new_day = lambda t: t.hour == 0 and new_hour(t)
    new_week = lambda t: t.weekday() == 0 and new_day(t)
    new_month = lambda t: t.day == 0 and new_day(t)

    class Timer:

    def run(self):
    t = datetime.now()
    diff = t.microsecond
    delay = 1.000000 - (0.000001 * diff)
    sleep(delay)
    self.onSecondCh ange()
    while True:
    sleep(1)
    t = datetime.now()
    self.onSecondCh ange()
    if new_minute(t): self.onMinuteCh ange()
    if new_hour(t): self.onHourChan ge()
    if new_day(t): self.onDayChang e()
    if new_week(t): self.onWeekChan ge()
    if new_month(t): self.onMonthCha nge()

    def onSecondChange( self): pass
    def onMinuteChange( self): pass
    def onHourChange(se lf): pass
    def onDayChange(sel f): pass
    def onWeekChange(se lf): pass
    def onMonthChange(s elf): pass
    def onYearChange(se lf): pass


    if __name__ == '__main__':
    class TestTimer(Timer , Thread):
    def onSecondChange( self):
    print "second elapse: %s" % datetime.now()
    def onMinuteChange( self):
    print "minute elapse : %s" % datetime.now()
    def onHourChange(se lf):
    print "hour elapse : %s" % datetime.now()
    t = TestTimer()
    t.start()

  • Jeremy Sanders

    #2
    Re: Force sleep to ignore interrupts

    andychambers200 2@yahoo.co.uk wrote:
    It works as I want when used in the main application thread.
    That is, when you hit Ctr + C, it stops running. However, if
    the class that subclasses it, also subclasses Thread, it breaks
    in that hitting Ctrl + C interrupts the call to sleep which puts
    the event loop out of sync with real time.
    Maybe you could install a signal handler to ignore ctrl+c, when required

    import signal
    signal.signal(s ignal.SIGINT, signal.SIG_IGN)


    --
    Jeremy Sanders

    Comment

    • andychambers2002@yahoo.co.uk

      #3
      Re: Force sleep to ignore interrupts

      Jeremy Sanders wrote:
      andychambers200 2@yahoo.co.uk wrote:
      >
      It works as I want when used in the main application thread.
      That is, when you hit Ctr + C, it stops running. However, if
      the class that subclasses it, also subclasses Thread, it breaks
      in that hitting Ctrl + C interrupts the call to sleep which puts
      the event loop out of sync with real time.
      >
      Maybe you could install a signal handler to ignore ctrl+c, when required
      >
      import signal
      signal.signal(s ignal.SIGINT, signal.SIG_IGN)
      That worked nicely. Thanks

      Andy

      Comment

      Working...