Skip to content

Commit cb19899

Browse files
committed
asyncio_priority.py Tidy up code.
1 parent 14618c7 commit cb19899

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

FASTPOLL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ list will never grow beyond this length.
396396
In the current implementation the callback takes no arguments. However it can
397397
be a bound method, enabling it to access class and instance variables.
398398

399-
No means of scheduling a high piority callback analogous to `call_soon` is
399+
No means of scheduling a high priority callback analogous to `call_soon` is
400400
provided. If such a mechanism existed, the cb would run immediately the coro
401401
yielded, with the coro being rescheduled once the cb returned `True`. This
402402
behaviour can be achieved more efficiently by simply calling the function.

asyncio_priority.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,15 @@ def __init__(self, runq_len=16, waitq_len=16, lpqlen=42):
3838
self.lpq = utimeq.utimeq(lpqlen)
3939
self.hp_tasks = []
4040

41-
# Schedule a single low priority task if one is ready or overdue.
42-
# The most overdue task is scheduled even if normal tasks are pending.
43-
# The most due task is scheduled only if no normal tasks are pending.
44-
def schedule_lp_task(self, cur_task, tnow):
45-
t = self.lpq.peektime()
46-
tim = time.ticks_diff(t, tnow)
47-
to_run = self._max_overdue_ms > 0 and tim < -self._max_overdue_ms
48-
if not to_run: # No overdue LP task.
49-
if len(self.runq): # zero delay tasks go straight to runq
50-
return False
51-
to_run = tim <= 0 # True if LP task is due
52-
if to_run and self.waitq: # Set False if a normal tasks is due.
53-
t = self.waitq.peektime()
54-
to_run = time.ticks_diff(t, tnow) > 0 # No normal task is ready
55-
if to_run:
56-
self.lpq.pop(cur_task)
57-
self.call_soon(cur_task[1], *cur_task[2])
58-
return True
59-
return False
60-
6141
def max_overdue_ms(self, t=None):
6242
if t is not None:
63-
self._max_overdue_ms = t
43+
self._max_overdue_ms = int(t)
6444
return self._max_overdue_ms
6545

6646
# Low priority versions of call_later() call_later_ms() and call_at_()
6747
def call_after_ms(self, delay, callback, *args):
6848
self.call_at_lp_(time.ticks_add(self.time(), delay), callback, *args)
6949

70-
7150
def call_after(self, delay, callback, *args):
7251
self.call_at_lp_(time.ticks_add(self.time(), int(delay * 1000)), callback, *args)
7352

@@ -85,24 +64,42 @@ def _schedule_hp(self, func, callback, *args):
8564
else:
8665
self.hp_tasks.append([func, callback, args])
8766

67+
# Low priority (LP) scheduling.
68+
# Schedule a single low priority task if one is ready or overdue.
69+
# The most overdue task is scheduled even if normal tasks are pending.
70+
# The most due task is scheduled only if no normal tasks are pending.
71+
8872
def run_forever(self):
8973
cur_task = [0, 0, 0]
9074
while True:
9175
tnow = self.time()
92-
# Schedule a LP task if no normal task is ready
93-
l = len(self.lpq)
94-
if (l and not self.schedule_lp_task(cur_task, tnow)) or l == 0:
95-
# Expire entries in waitq and move them to runq
96-
while self.waitq:
97-
t = self.waitq.peektime()
98-
delay = time.ticks_diff(t, tnow)
99-
if delay > 0:
100-
break
101-
self.waitq.pop(cur_task)
102-
if __debug__ and DEBUG:
103-
log.debug("Moving from waitq to runq: %s", cur_task[1])
76+
if self.lpq:
77+
# Schedule a LP task if overdue or if no normal task is ready
78+
to_run = False # Assume no LP task is to run
79+
t = self.lpq.peektime()
80+
tim = time.ticks_diff(t, tnow)
81+
to_run = self._max_overdue_ms > 0 and tim < -self._max_overdue_ms
82+
if not (to_run or self.runq): # No overdue LP task or task on runq
83+
# zero delay tasks go straight to runq. So don't schedule LP if runq
84+
to_run = tim <= 0 # True if LP task is due
85+
if to_run and self.waitq: # Set False if normal tasks due.
86+
t = self.waitq.peektime()
87+
to_run = time.ticks_diff(t, tnow) > 0 # No normal task is ready
88+
if to_run:
89+
self.lpq.pop(cur_task)
10490
self.call_soon(cur_task[1], *cur_task[2])
10591

92+
# Expire entries in waitq and move them to runq
93+
while self.waitq:
94+
t = self.waitq.peektime()
95+
delay = time.ticks_diff(t, tnow)
96+
if delay > 0:
97+
break
98+
self.waitq.pop(cur_task)
99+
if __debug__ and DEBUG:
100+
log.debug("Moving from waitq to runq: %s", cur_task[1])
101+
self.call_soon(cur_task[1], *cur_task[2])
102+
106103
# Process runq
107104
l = len(self.runq)
108105
if __debug__ and DEBUG:

0 commit comments

Comments
 (0)