Sun, 28 Jan 2018 11:54:46 +0000
Better package-like organisation
49 | 1 | # |
2 | # Scheduler for Borgend | |
3 | # | |
4 | # This module simply provide a way for other threads to until a given time | |
5 | # | |
6 | ||
7 | import time | |
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
8 | from threading import Condition, Thread |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
9 | |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
10 | from . import loggers |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
11 | from . import dreamtime |
49 | 12 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
13 | logger=loggers.get(__name__) |
49 | 14 | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
15 | class QueuedEvent: |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
16 | def __init__(self, cond, name=None): |
49 | 17 | self.next=None |
18 | self.prev=None | |
19 | self.name=name | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
20 | self.cond=cond |
49 | 21 | |
22 | def __lt__(self, other): | |
54 | 23 | raise NotImplementedError |
49 | 24 | |
25 | def insert_after(self, ev): | |
69
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
26 | if not self.next or ev<self.next: |
49 | 27 | self.insert_immediately_after(ev) |
28 | else: | |
29 | self.next.insert_after(ev) | |
30 | ||
31 | def insert_immediately_after(self, ev): | |
69
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
32 | assert(ev.next is None and ev.prev is None) |
49 | 33 | ev.prev=self |
34 | ev.next=self.next | |
35 | self.next=ev | |
36 | ||
69
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
37 | def insert_immediately_before(self, ev): |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
38 | assert(ev.next is None and ev.prev is None) |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
39 | ev.next=self |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
40 | ev.prev=self.prev |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
41 | self.prev=ev |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
42 | |
49 | 43 | def unlink(self): |
44 | n=self.next | |
45 | p=self.prev | |
46 | if n: | |
47 | n.prev=p | |
48 | if p: | |
49 | p.next=n | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
50 | self.next=None |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
51 | self.prev=None |
49 | 52 | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
53 | class ScheduledEvent(QueuedEvent): |
78
83b43987e61e
Renamed the "sleep" module "dreamtime"
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
54 | #@accepts(ScheduledEvent, dreamtime.Time, threading.Cond, str) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
55 | def __init__(self, when, cond, name=None): |
54 | 56 | super().__init__(cond, name=name) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
57 | self.when=when |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
58 | |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
59 | def __lt__(self, other): |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
60 | return self.when < other.when |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
61 | |
49 | 62 | class TerminableThread(Thread): |
63 | def __init__(self, *args, **kwargs): | |
64 | super().__init__(*args, **kwargs) | |
65 | self._terminate=False | |
66 | self._cond=Condition() | |
67 | ||
68 | def terminate(self): | |
69 | with self._cond: | |
70 | _terminate=True | |
71 | self._cond.notify() | |
72 | ||
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
73 | class QueueThread(TerminableThread): |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
74 | def __init__(self, *args, **kwargs): |
54 | 75 | super().__init__(*args, **kwargs) |
76 | self.daemon = True | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
77 | self._list = None |
49 | 78 | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
79 | def _insert(self, ev): |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
80 | if not self._list: |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
81 | #logger.debug("Insert first") |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
82 | self._list=ev |
54 | 83 | elif ev<self._list: |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
84 | #logger.debug("Insert beginning") |
69
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
85 | self._list.insert_immediately_before(ev) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
86 | self._list=ev |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
87 | else: |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
88 | #logger.debug("Insert after") |
59
8d0a815022cc
Oops, accidentally calling the wrong function (+log message clarification)
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
89 | self._list.insert_after(ev) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
90 | |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
91 | self._cond.notify() |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
92 | |
54 | 93 | def _unlink(self, ev): |
94 | if ev==self._list: | |
95 | self._list=ev.next | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
96 | ev.unlink() |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
97 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
98 | def _resort(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
99 | oldlist=self._list |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
100 | self._list=None |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
101 | while oldlist: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
102 | ev=oldlist |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
103 | oldlist=oldlist.next |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
104 | ev.unlink() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
105 | self._insert(ev) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
106 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
107 | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
108 | |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
109 | class Scheduler(QueueThread): |
49 | 110 | # Default to precision of 60 seconds: the scheduler thread will never |
111 | # sleep longer than that, to get quickly back on track with the schedule | |
112 | # when the computer wakes up from sleep | |
113 | def __init__(self, precision=60): | |
114 | self.precision = precision | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
115 | self._next_event_time = None |
54 | 116 | super().__init__(target = self._scheduler_thread, name = 'Scheduler') |
78
83b43987e61e
Renamed the "sleep" module "dreamtime"
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
117 | dreamtime.add_callback(self, self._wakeup_callback) |
49 | 118 | |
54 | 119 | def _scheduler_thread(self): |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
120 | logger.debug("Scheduler thread started") |
49 | 121 | with self._cond: |
122 | while not self._terminate: | |
123 | now = time.monotonic() | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
124 | if not self._list: |
49 | 125 | timeout = None |
126 | else: | |
127 | # Wait at most precision seconds, or until next event if it | |
128 | # comes earlier | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
129 | timeout=min(self.precision, self._list.when.realtime()-now) |
49 | 130 | |
131 | if not timeout or timeout>0: | |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
132 | logger.debug("Scheduler waiting %d seconds" % (timeout or (-1))) |
49 | 133 | self._cond.wait(timeout) |
134 | now = time.monotonic() | |
135 | ||
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
136 | logger.debug("Scheduler timed out") |
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
137 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
138 | while self._list and self._list.when.monotonic() <= now: |
54 | 139 | ev=self._list |
59
8d0a815022cc
Oops, accidentally calling the wrong function (+log message clarification)
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
140 | logger.debug("Scheduler activating %s" % (ev.name or "(unknown)")) |
54 | 141 | # We are only allowed to remove ev from list when ev.cond allows |
142 | with ev.cond: | |
143 | self._list=ev.next | |
144 | ev.unlink() | |
145 | ev.cond.notifyAll() | |
146 | ||
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
147 | def _wakeup_callback(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
148 | logger.debug("Rescheduling events after wakeup") |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
149 | with self._cond: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
150 | self._resort() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
151 | |
54 | 152 | def _wait(self, ev): |
153 | with self._cond: | |
154 | self._insert(ev) | |
155 | ||
156 | # This will release the lock on cond, allowing queue manager (scheduler) | |
157 | # thread to notify us if we are already to be released | |
158 | ev.cond.wait() | |
159 | ||
160 | # If we were woken up by some other event, not the scheduler, | |
161 | # ensure the event is removed | |
162 | with self._cond: | |
163 | self._unlink(ev) | |
49 | 164 | |
165 | # cond has to be acquired on entry! | |
166 | def wait_until(self, when, cond, name=None): | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
167 | logger.debug("Scheduling '%s' in %s seconds [%s]" % |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
168 | (name, when.seconds_to(), when.__class__.__name__)) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
169 | self._wait(ScheduledEvent(when, cond, name)) |
49 | 170 |