Proxies
pykka.ActorProxy
Bases: Generic[A]
A proxy object for an actor's attributes and methods, via an ActorRef.
The proxy allows the referenced actor to be used through regular method calls and field access.
Creating a proxy
You can create an ActorProxy from any
ActorRef:
actor_ref = MyActor.start()
actor_proxy = ActorProxy(actor_ref)
You can also get an ActorProxy by using
proxy():
actor_proxy = MyActor.start().proxy()
Attributes and method calls
When reading an attribute or getting a return value from a method, you get a
Future object back. To get the enclosed value from the
future, you must call get() on the returned future:
print(actor_proxy.string_attribute.get())
print(actor_proxy.count().get() + 1)
If you call a method just for it's side effects and do not care about the
return value, you do not need to accept the returned future or call
get() on the future. Simply call the method, and it
will be executed concurrently with your own code:
actor_proxy.method_with_side_effect()
If you want to block your own code from continuing while the other method
is processing, you can use get() to block until it
completes::
actor_proxy.method_with_side_effect().get()
You can also use the await keyword to block until the method completes:
await actor_proxy.method_with_side_effect()
If you access a proxied method as an attribute, without calling it, you
get an CallableProxy.
Proxy to itself
An actor can use a proxy to itself to schedule work for itself. The scheduled work will only be done after the current message and all messages already in the inbox are processed.
For example, if an actor can split a time consuming task into multiple parts, and after completing each part can ask itself to start on the next part using proxied calls or messages to itself, it can react faster to other incoming messages as they will be interleaved with the parts of the time consuming task. This is especially useful for being able to stop the actor in the middle of a time consuming task.
To create a proxy to yourself, use the actor's
actor_ref attribute:
proxy_to_myself_in_the_future = self.actor_ref.proxy()
If you create a proxy in your actor's constructor or
on_start() method, you can create a nice API for
deferring work to yourself in the future:
def __init__(self):
...
self._in_future = self.actor_ref.proxy()
...
def do_work(self):
...
self._in_future.do_more_work()
...
def do_more_work(self):
...
To avoid infinite loops during proxy introspection, proxies to self
should be kept as private instance attributes by prefixing the attribute
name with _.
Example
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "pykka",
# ]
# ///
import pykka
class Adder(pykka.ThreadingActor):
def add_one(self, i: int) -> int:
print(f"Adder is adding 1 to {i}")
return i + 1
class Bookkeeper(pykka.ThreadingActor):
def __init__(self, adder: pykka.ActorProxy[Adder]) -> None:
super().__init__()
self.adder = adder
def count_to(self, target: int) -> None:
i = 0
while i < target:
i = self.adder.add_one(i).get()
print(f"Bookkeeper got {i} back")
if __name__ == "__main__":
# Start the adder actor
adder = Adder.start().proxy()
# Start the bookkeeper actor, passing it the adder actor's proxy
bookkeeper = Bookkeeper.start(adder).proxy()
# Ask the bookkeeper to count to 5
bookkeeper.count_to(5).get()
# Stop all running actors using the ActorRegistry
pykka.ActorRegistry.stop_all()
Parameters:
-
actor_ref(ActorRef[A]) –reference to the actor to proxy
Raises:
-
ActorDeadError–if the actor is not alive
Source code in src/pykka/_proxy.py
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
actor_ref
instance-attribute
The actor's pykka.ActorRef instance.
__getattr__
Get a field or callable from the actor.
Source code in src/pykka/_proxy.py
__setattr__
Set a field on the actor.
Blocks until the field is set to check if any exceptions was raised.
Source code in src/pykka/_proxy.py
pykka.CallableProxy
Bases: Generic[A]
Proxy to a single method.
CallableProxy instances are returned when accessing
methods on a ActorProxy without calling them.
Example
Source code in src/pykka/_proxy.py
__call__
Call with ask() semantics.
Returns a future which will yield the called method's return value.
If the call raises an exception it is set on the future, and the exception is
reraised by get(). If the future is left unused,
the exception will not be reraised. Either way, the exception will
also be logged. See the logging section in the docs for details.
Source code in src/pykka/_proxy.py
defer
Call with tell() semantics.
Does not create or return a future.
If the call raises an exception, there is no future to set the
exception on. Thus, the actor's on_failure()
hook is called instead.
Version added: Pykka 2.0
Source code in src/pykka/_proxy.py
pykka.traversable
Mark an actor attribute as traversable.
The traversable marker makes the actor attribute's own methods and
attributes available to users of the actor through an
ActorProxy.
Used as a function to mark a single attribute:
class AnActor(pykka.ThreadingActor):
playback = pykka.traversable(Playback())
class Playback(object):
def play(self):
return True
This function can also be used as a class decorator, making all instances of the class traversable:
class AnActor(pykka.ThreadingActor):
playback = Playback()
@pykka.traversable
class Playback(object):
def play(self):
return True
The third alternative, and the only way in Pykka < 2.0, is to manually
mark a class as traversable by setting the pykka_traversable attribute
to True:
class AnActor(pykka.ThreadingActor):
playback = Playback()
class Playback(object):
pykka_traversable = True
def play(self):
return True
When the attribute is marked as traversable, its methods can be executed in the context of the actor through an actor proxy::
proxy = AnActor.start().proxy()
assert proxy.playback.play().get() is True
Version added: Pykka 2.0