2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 from __future__ import absolute_import, print_function
13 from .config import actor_class
15 class TimedCallBackActor(actor_class):
16 """Send messages to other actors on a schedule.
18 Other actors can call the schedule() method to schedule delivery of a
19 message at a later time. This actor runs the necessary event loop for
22 def __init__(self, max_sleep=1, timefunc=None):
23 super(TimedCallBackActor, self).__init__()
24 self._proxy = self.actor_ref.tell_proxy()
26 self.max_sleep = max_sleep
28 self._timefunc = time.time
30 self._timefunc = timefunc
32 def schedule(self, delivery_time, receiver, *args, **kwargs):
35 heapq.heappush(self.messages, (delivery_time, receiver, args, kwargs))
40 til_next = self.messages[0][0] - self._timefunc()
42 t, receiver, args, kwargs = heapq.heappop(self.messages)
44 receiver(*args, **kwargs)
45 except pykka.ActorDeadError:
48 time.sleep(min(til_next, self.max_sleep))