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):
23 super(TimedCallBackActor, self).__init__()
24 self._proxy = self.actor_ref.tell_proxy()
26 self.max_sleep = max_sleep
28 def schedule(self, delivery_time, receiver, *args, **kwargs):
31 heapq.heappush(self.messages, (delivery_time, receiver, args, kwargs))
36 til_next = self.messages[0][0] - time.time()
38 t, receiver, args, kwargs = heapq.heappop(self.messages)
40 receiver(*args, **kwargs)
41 except pykka.ActorDeadError:
44 time.sleep(min(til_next, self.max_sleep))