2020-11-17 10:54:18 +01:00
|
|
|
"""
|
2020-12-20 15:58:53 +01:00
|
|
|
Various object wrappers and abstraction layers for internal use
|
2020-11-17 10:54:18 +01:00
|
|
|
|
|
|
|
Copyright (C) 2020 nocturn9x
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
2021-06-08 17:21:59 +02:00
|
|
|
from giambio.task import Task
|
|
|
|
from heapq import heappush, heappop
|
2020-11-17 10:54:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TimeQueue:
|
|
|
|
"""
|
|
|
|
An abstraction layer over a heap queue based on time. This is where
|
|
|
|
sleeping tasks will be put when they are not running
|
2020-12-20 15:58:53 +01:00
|
|
|
|
|
|
|
:param clock: The same monotonic clock that was passed to the thread-local event loop.
|
2021-06-03 16:34:26 +02:00
|
|
|
It is important for the queue to be synchronized with the loop as this allows
|
|
|
|
the sleeping mechanism to work reliably
|
2020-11-17 10:54:18 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, clock):
|
|
|
|
"""
|
|
|
|
Object constructor
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.clock = clock
|
2020-12-05 17:09:59 +01:00
|
|
|
# The sequence number handles the race condition
|
2020-12-20 15:58:53 +01:00
|
|
|
# of two tasks with identical deadlines, acting
|
2020-12-05 17:09:59 +01:00
|
|
|
# as a tie breaker
|
2020-12-20 15:58:53 +01:00
|
|
|
self.sequence = 0
|
|
|
|
self.container: List[Tuple[float, int, Task]] = []
|
2020-11-17 10:54:18 +01:00
|
|
|
|
|
|
|
def __contains__(self, item):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements item in self. This method behaves
|
|
|
|
as if the queue only contained tasks and ignores
|
|
|
|
their timeouts and tiebreakers
|
|
|
|
"""
|
|
|
|
|
2020-12-19 15:18:12 +01:00
|
|
|
for i in self.container:
|
|
|
|
if i[2] == item:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
def index(self, item):
|
|
|
|
"""
|
|
|
|
Returns the index of the given item in the list
|
|
|
|
or -1 if it is not present
|
|
|
|
"""
|
|
|
|
|
2021-04-23 09:17:55 +02:00
|
|
|
for i, e in enumerate(self.container):
|
|
|
|
if e[2] == item:
|
2020-12-20 15:58:53 +01:00
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
|
|
|
def discard(self, item):
|
|
|
|
"""
|
|
|
|
Discards an item from the queue and
|
|
|
|
calls heapify(self.container) to keep
|
|
|
|
the heap invariant if an element is removed.
|
|
|
|
This method does nothing if the item is not
|
|
|
|
in the queue, but note that in this case the
|
|
|
|
operation would still take O(n) iterations
|
|
|
|
to complete
|
|
|
|
|
|
|
|
:param item: The item to be discarded
|
|
|
|
"""
|
|
|
|
|
|
|
|
idx = self.index(item)
|
2021-04-23 09:17:55 +02:00
|
|
|
if idx != -1:
|
|
|
|
self.container.pop(idx)
|
2020-12-20 15:58:53 +01:00
|
|
|
heapify(self.container)
|
|
|
|
|
|
|
|
def get_closest_deadline(self) -> float:
|
|
|
|
"""
|
|
|
|
Returns the closest deadline that is meant to expire
|
|
|
|
or raises IndexError if the queue is empty
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self:
|
|
|
|
raise IndexError("TimeQueue is empty")
|
|
|
|
return self.container[0][0]
|
2020-11-17 10:54:18 +01:00
|
|
|
|
|
|
|
def __iter__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements iter(self)
|
|
|
|
"""
|
|
|
|
|
2020-11-22 14:35:07 +01:00
|
|
|
return self
|
2020-12-19 22:09:48 +01:00
|
|
|
|
2020-11-22 14:35:07 +01:00
|
|
|
def __next__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements next(self)
|
|
|
|
"""
|
|
|
|
|
2020-11-22 14:35:07 +01:00
|
|
|
try:
|
|
|
|
return self.get()
|
|
|
|
except IndexError:
|
|
|
|
raise StopIteration from None
|
2020-11-17 10:54:18 +01:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements self[n]
|
|
|
|
"""
|
|
|
|
|
2020-11-17 10:54:18 +01:00
|
|
|
return self.container.__getitem__(item)
|
|
|
|
|
|
|
|
def __bool__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements bool(self)
|
|
|
|
"""
|
|
|
|
|
2020-11-17 10:54:18 +01:00
|
|
|
return bool(self.container)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements repr(self) and str(self)
|
|
|
|
"""
|
|
|
|
|
2020-11-17 10:54:18 +01:00
|
|
|
return f"TimeQueue({self.container}, clock={self.clock})"
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
def put(self, task: Task, amount: float):
|
2020-11-17 10:54:18 +01:00
|
|
|
"""
|
2020-12-20 15:58:53 +01:00
|
|
|
Pushes a task onto the queue together with its
|
|
|
|
sleep amount
|
|
|
|
|
|
|
|
:param task: The task that is meant to sleep
|
|
|
|
:type task: :class: Task
|
|
|
|
:param amount: The amount of time, in seconds, that the
|
|
|
|
task should sleep for
|
|
|
|
:type amount: float
|
2020-11-17 10:54:18 +01:00
|
|
|
"""
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
heappush(self.container, (self.clock() + amount, self.sequence, task))
|
2020-11-17 10:54:18 +01:00
|
|
|
self.sequence += 1
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
def get(self) -> Task:
|
2020-11-17 10:54:18 +01:00
|
|
|
"""
|
|
|
|
Gets the first task that is meant to run
|
2020-12-20 15:58:53 +01:00
|
|
|
|
|
|
|
:raises: IndexError if the queue is empty
|
2020-11-17 10:54:18 +01:00
|
|
|
"""
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
if not self.container:
|
|
|
|
raise IndexError("get from empty TimeQueue")
|
2020-11-28 13:04:27 +01:00
|
|
|
return heappop(self.container)[2]
|
2020-12-05 17:09:59 +01:00
|
|
|
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
class DeadlinesQueue:
|
2020-12-05 17:09:59 +01:00
|
|
|
"""
|
|
|
|
An ordered queue for storing tasks deadlines
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Object constructor
|
|
|
|
"""
|
|
|
|
|
2020-12-19 15:18:12 +01:00
|
|
|
self.pools = set()
|
2020-12-20 15:58:53 +01:00
|
|
|
self.container: List[Tuple[float, int, giambio.context.TaskManager]] = []
|
|
|
|
self.sequence = 0
|
2020-12-05 17:09:59 +01:00
|
|
|
|
|
|
|
def __contains__(self, item):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements item in self. This method behaves
|
|
|
|
as if the queue only contained tasks and ignores
|
|
|
|
their timeouts and tiebreakers
|
|
|
|
"""
|
|
|
|
|
|
|
|
for i in self.container:
|
|
|
|
if i[2] == item:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def index(self, item):
|
|
|
|
"""
|
|
|
|
Returns the index of the given item in the list
|
|
|
|
or -1 if it is not present
|
|
|
|
"""
|
|
|
|
|
2021-04-23 09:17:55 +02:00
|
|
|
for i, e in enumerate(self.container):
|
|
|
|
if e[2] == item:
|
2020-12-20 15:58:53 +01:00
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
|
|
|
def discard(self, item):
|
|
|
|
"""
|
|
|
|
Discards an item from the queue and
|
|
|
|
calls heapify(self.container) to keep
|
|
|
|
the heap invariant if an element is removed.
|
|
|
|
This method does nothing if the item is not
|
|
|
|
in the queue, but note that in this case the
|
|
|
|
operation would still take O(n) iterations
|
|
|
|
to complete
|
|
|
|
|
|
|
|
:param item: The item to be discarded
|
|
|
|
"""
|
|
|
|
|
|
|
|
idx = self.index(item)
|
|
|
|
if idx != 1:
|
2021-04-23 09:17:55 +02:00
|
|
|
self.container.pop(idx)
|
2020-12-20 15:58:53 +01:00
|
|
|
heapify(self.container)
|
|
|
|
|
|
|
|
def get_closest_deadline(self) -> float:
|
|
|
|
"""
|
|
|
|
Returns the closest deadline that is meant to expire
|
2021-06-08 17:21:59 +02:00
|
|
|
or returns 0.0 if the queue is empty
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not self:
|
2021-06-08 17:21:59 +02:00
|
|
|
return 0.0
|
2020-12-20 15:58:53 +01:00
|
|
|
return self.container[0][0]
|
2020-12-05 17:09:59 +01:00
|
|
|
|
|
|
|
def __iter__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements iter(self)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self
|
2020-12-05 17:09:59 +01:00
|
|
|
|
|
|
|
def __next__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements next(self)
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
return self.get()
|
|
|
|
except IndexError:
|
|
|
|
raise StopIteration from None
|
2020-12-05 17:09:59 +01:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements self[n]
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.container.__getitem__(item)
|
2020-12-05 17:09:59 +01:00
|
|
|
|
|
|
|
def __bool__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements bool(self)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return bool(self.container)
|
2020-12-05 17:09:59 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2020-12-20 15:58:53 +01:00
|
|
|
"""
|
|
|
|
Implements repr(self) and str(self)
|
|
|
|
"""
|
|
|
|
|
2020-12-05 17:09:59 +01:00
|
|
|
return f"DeadlinesQueue({self.container})"
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
def put(self, pool: "giambio.context.TaskManager"):
|
2020-12-05 17:09:59 +01:00
|
|
|
"""
|
2020-12-20 15:58:53 +01:00
|
|
|
Pushes a pool with its deadline onto the queue. The
|
|
|
|
timeout amount will be inferred from the pool object
|
|
|
|
itself
|
|
|
|
|
|
|
|
:param pool: The pool object to store
|
2020-12-05 17:09:59 +01:00
|
|
|
"""
|
|
|
|
|
2020-12-19 15:18:12 +01:00
|
|
|
if pool not in self.pools:
|
|
|
|
self.pools.add(pool)
|
2020-12-20 15:58:53 +01:00
|
|
|
heappush(self.container, (pool.timeout, self.sequence, pool))
|
|
|
|
self.sequence += 1
|
2020-12-05 17:09:59 +01:00
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
def get(self) -> "giambio.context.TaskManager":
|
2020-12-05 17:09:59 +01:00
|
|
|
"""
|
2020-12-20 15:58:53 +01:00
|
|
|
Gets the first pool that is meant to expire
|
|
|
|
|
|
|
|
:raises: IndexError if the queue is empty
|
2020-12-05 17:09:59 +01:00
|
|
|
"""
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
if not self.container:
|
|
|
|
raise IndexError("get from empty DeadlinesQueue")
|
2020-12-19 15:18:12 +01:00
|
|
|
d = heappop(self.container)
|
|
|
|
self.pools.discard(d[2])
|
2020-12-20 15:58:53 +01:00
|
|
|
return d[2]
|