Timer module?
SUZUKI Hisao
suzuki at acm.org
Sat Feb 3 02:10:00 EST 2001
In article <3A76F442.7BB0B9A9 at americasm01.nt.com>, Rick wrote:
> I wonder if there is already any timer module out there that allows me
> to:
>
> - set a timer without blocking the current thread
I have written such one :^)
> - cancel the timer
Yes.
> - maybe also, able to tell it to recur at a given interval for so many
> times; and able to change that
Yes, by re-scheduling it (= a Task instance) explicitly.
> - create thousands of timers with a performance penalty that grows
> slower than linearly with the number of timers; preferably a performance
> cost that is small and almost constant
Probably yes; O(1) at best and O(n) at worst theoretically.
The module is pypage/lib/timer.py, which is a part of PyPage
1.4, a handy multi-threaded Web server with JSP-like features.
I wrote and posted it to a newsgroup in Japan last year:
| Newsgroups: fj.sources
| From: SUZUKI Hisao <suzuki at acm.org>
| Subject: PyPage 1.4
| Date: Sun, 12 Nov 2000 23:20:41 +0900
| Message-ID: <8um90n$fc9$1 at nwms2.odn.ne.jp>
You can get it also via HTTP (thanks to TAKAHASHI Hideaki):
http://www.zob.ne.jp/~hide-t/comp/python/pypage/pypage-1.4.tar.bz2
It is a free software WITHOUT ANY WARRANTY. Anyone may freely
use, alter and redistribute it. All I ask is a brief mention of
me as the original author in the source or accompanying file(s).
However, the documentation is in Japanese.
If you don't mind it, I introduce the module to you. First,
copy two files, timer.py and jthread.py, from pypage/lib/ to the
current directory. Then, execute the script:
----------------------------------------------------------------
#!/usr/bin/env python
import timer
def foo(msg):
print msg
task.when += 3
timer.schedule(task) # re-schedule the task
# enable the timer
timer.start()
# create and schedule a task to call foo("spam") 3 seconds later
task = timer.after(3, foo, "spam")
raw_input('hit "Enter" to stop\n')
----------------------------------------------------------------
It will repeat "spam" until you hit Enter key.
Hope this helps.
--
SUZUKI Hisao >>> def fib(n): return reduce(lambda x, y:
suzuki at acm.org ... (x,x[0][-1]+x[1]), [()]*n, ((0L,),1L))
More information about the Python-list
mailing list