/usr/lib/python3/dist-packages/pykka/registry.py is in python3-pykka 1.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | import logging as _logging
import threading as _threading
try:
_basestring = basestring
except NameError:
# Python 3
_basestring = str
_logger = _logging.getLogger('pykka')
class ActorRegistry(object):
"""
Registry which provides easy access to all running actors.
Contains global state, but should be thread-safe.
"""
_actor_refs = []
_actor_refs_lock = _threading.RLock()
@classmethod
def broadcast(cls, message, target_class=None):
"""
Broadcast ``message`` to all actors of the specified ``target_class``.
If no ``target_class`` is specified, the message is broadcasted to all
actors.
:param message: the message to send
:type message: picklable dict
:param target_class: optional actor class to broadcast the message to
:type target_class: class or class name
"""
if isinstance(target_class, _basestring):
targets = cls.get_by_class_name(target_class)
elif target_class is not None:
targets = cls.get_by_class(target_class)
else:
targets = cls.get_all()
for ref in targets:
ref.tell(message)
@classmethod
def get_all(cls):
"""
Get :class:`ActorRef <pykka.ActorRef>` for all running actors.
:returns: list of :class:`pykka.ActorRef`
"""
with cls._actor_refs_lock:
return cls._actor_refs[:]
@classmethod
def get_by_class(cls, actor_class):
"""
Get :class:`ActorRef` for all running actors of the given class, or of
any subclass of the given class.
:param actor_class: actor class, or any superclass of the actor
:type actor_class: class
:returns: list of :class:`pykka.ActorRef`
"""
with cls._actor_refs_lock:
return [
ref for ref in cls._actor_refs
if issubclass(ref.actor_class, actor_class)]
@classmethod
def get_by_class_name(cls, actor_class_name):
"""
Get :class:`ActorRef` for all running actors of the given class
name.
:param actor_class_name: actor class name
:type actor_class_name: string
:returns: list of :class:`pykka.ActorRef`
"""
with cls._actor_refs_lock:
return [
ref for ref in cls._actor_refs
if ref.actor_class.__name__ == actor_class_name]
@classmethod
def get_by_urn(cls, actor_urn):
"""
Get an actor by its universally unique URN.
:param actor_urn: actor URN
:type actor_urn: string
:returns: :class:`pykka.ActorRef` or :class:`None` if not found
"""
with cls._actor_refs_lock:
refs = [
ref for ref in cls._actor_refs
if ref.actor_urn == actor_urn]
if refs:
return refs[0]
@classmethod
def register(cls, actor_ref):
"""
Register an :class:`ActorRef` in the registry.
This is done automatically when an actor is started, e.g. by calling
:meth:`Actor.start() <pykka.Actor.start>`.
:param actor_ref: reference to the actor to register
:type actor_ref: :class:`pykka.ActorRef`
"""
with cls._actor_refs_lock:
cls._actor_refs.append(actor_ref)
_logger.debug('Registered %s', actor_ref)
@classmethod
def stop_all(cls, block=True, timeout=None):
"""
Stop all running actors.
``block`` and ``timeout`` works as for
:meth:`ActorRef.stop() <pykka.ActorRef.stop>`.
If ``block`` is :class:`True`, the actors are guaranteed to be stopped
in the reverse of the order they were started in. This is helpful if
you have simple dependencies in between your actors, where it is
sufficient to shut down actors in a LIFO manner: last started, first
stopped.
If you have more complex dependencies in between your actors, you
should take care to shut them down in the required order yourself, e.g.
by stopping dependees from a dependency's
:meth:`on_stop() <pykka.Actor.on_stop>` method.
:returns: If not blocking, a list with a future for each stop action.
If blocking, a list of return values from
:meth:`pykka.ActorRef.stop`.
"""
return [ref.stop(block, timeout) for ref in reversed(cls.get_all())]
@classmethod
def unregister(cls, actor_ref):
"""
Remove an :class:`ActorRef <pykka.ActorRef>` from the registry.
This is done automatically when an actor is stopped, e.g. by calling
:meth:`Actor.stop() <pykka.Actor.stop>`.
:param actor_ref: reference to the actor to unregister
:type actor_ref: :class:`pykka.ActorRef`
"""
removed = False
with cls._actor_refs_lock:
if actor_ref in cls._actor_refs:
cls._actor_refs.remove(actor_ref)
removed = True
if removed:
_logger.debug('Unregistered %s', actor_ref)
else:
_logger.debug(
'Unregistered %s (not found in registry)', actor_ref)
|