Thread that executes a task every N seconds - sample implementation
Itamar S.-T.
itamarst at yahoo.com
Mon Apr 30 10:39:35 EDT 2001
Hi,
When developing a threaded program I found a common
pattern is a thread that does something every N
seconds. For example, record all logged in users once
a minute. Now, when we shutdown this thread we want it
to stop immediatly if it's sleeping, or finish
whatever its doing and then stop if it's not sleeping.
The following class does this, and saves you the work
of writing the same code over and over.
Suggestions for alternate implementations are welcome.
An example of using the class:
====================================
class Demo(TaskThread):
"""Say 'hello' once a minute"""
def __init__(self):
TaskThread.__init__(self)
self.setInterval(60)
def task(self):
"""This will be executed every 60 seconds"""
print "hello"
t = Demo()
t.start()
# do other stufff, lalalalala
t.shutdown() # stop Demo thread
=====
Itamar Shtull-Trauring, itamar(at)shtull-trauring.org
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: taskthread.py
Type: application/x-unknown
Size: 822 bytes
Desc: taskthread.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20010430/5638fe60/attachment.bin>
More information about the Python-list
mailing list