473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.onSecondChange()
while True:
sleep(1)
t = datetime.now()
self.onSecondChange()
if new_minute(t): self.onMinuteChange()
if new_hour(t): self.onHourChange()
if new_day(t): self.onDayChange()
if new_week(t): self.onWeekChange()
if new_month(t): self.onMonthChange()

def onSecondChange(self): pass
def onMinuteChange(self): pass
def onHourChange(self): pass
def onDayChange(self): pass
def onWeekChange(self): pass
def onMonthChange(self): pass
def onYearChange(self): 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(self):
print "hour elapse : %s" % datetime.now()
t = TestTimer()
t.start()

Sep 14 '06 #1
2 1703
an**************@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(signal.SIGINT, signal.SIG_IGN)
--
Jeremy Sanders
http://www.jeremysanders.net/
Sep 14 '06 #2
Jeremy Sanders wrote:
an**************@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(signal.SIGINT, signal.SIG_IGN)
That worked nicely. Thanks

Andy

Sep 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BOOGIEMAN | last post by:
I have line "time.sleep(60)" in my code How do I cancel waiting 60 seconds if I want to continue with program imediately ? Like "Press some button if you don't want to wait" If it can't be...
0
by: anders thoresson | last post by:
Hi, I have this function I call everytime I need to make a query from within my php-scripts: function db_connect ($user, $pwd, $db, $debug = 0) { $link = @mysql_pconnect("localhost",...
5
by: Erich Schreiber | last post by:
In the Python Library Reference the explanation of the time.sleep() function reads amongst others: > The actual suspension time may be less than that requested because > any caught signal will...
1
by: Yin | last post by:
I am writing a script that monitors a child process. If the child process dies on its own, then the parent continues on. If the child process is still alive after a timeout period, the parent...
8
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the...
10
by: syed | last post by:
I am trying to write a little program that goes to sleep for 5 seconds. However, if the user presses Ctrl-C, it interrupts the sleep and prints the unslept amount. For example: unix>...
4
by: jmarcrum | last post by:
I have to modify this assembly code in order to make it use interrupts. Right now, when I push button 1 on the microprocessor the count increments once (the LED's count in binary, using only 3 LED's...
41
by: Twayne | last post by:
Hi, How would I go about "forcing" a user from one page to another? I have a very simple random question/answer entrance requirement for an e-mail form. After 3 page views most people are...
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.