]>
code.delx.au - bg-scripts/blob - lib/asyncsched.py
c021fc108be8d49d1ef3fdc7c8efec9f4c84cc1d
1 # Copyright 2008 James Bunton <jamesbunton@fastmail.fm>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3 # asyncore.loop() with delayed function calls
12 def __init__(self
, delay
, func
, args
=[], kwargs
={}):
13 self
.time
= time
.time() + delay
14 self
.func
= lambda: func(*args
, **kwargs
)
16 def __cmp__(self
, other
):
17 return cmp(self
.time
, other
.time
)
26 assert self
.func
is not None
29 def schedule(delay
, func
, args
=[], kwargs
={}):
30 task
= Task(delay
, func
, args
, kwargs
)
31 heapq
.heappush(tasks
, task
)
34 def loop(timeout
=30.0):
37 while tasks
and tasks
[0].time
< now
:
38 task
= heapq
.heappop(tasks
)
43 t
= max(min(t
, tasks
[0].time
- now
), 0)
45 asyncore
.poll(timeout
=t
)