Actors
pykka.Actor
Bases: ABC
An actor is an execution unit that executes concurrently with other actors.
To create an actor:
-
subclass one of the
Actorimplementations: -
implement your methods, including
__init__(), as usual, - call
Actor.start()on your actor class, passing the method any arguments for your constructor.
To stop an actor, call Actor.stop() or
ActorRef.stop().
Example
import pykka
class MyActor(pykka.ThreadingActor):
def __init__(self, my_arg=None):
super().__init__()
... # My optional init code with access to start() arguments
def on_start(self):
... # My optional setup code in same context as on_receive()
def on_stop(self):
... # My optional cleanup code in same context as on_receive()
def on_failure(self, exception_type, exception_value, traceback):
... # My optional cleanup code in same context as on_receive()
def on_receive(self, message):
... # My optional message handling code for a plain actor
def a_method(self, ...):
... # My regular method to be used through an ActorProxy
my_actor_ref = MyActor.start(my_arg=...)
my_actor_ref.stop()
Source code in src/pykka/_actor.py
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
actor_inbox
instance-attribute
The actor's inbox.
Use ActorRef.tell(),
ActorRef.ask(), and friends to put messages in the
inbox.
actor_stopped
instance-attribute
A threading.Event representing whether or not the actor
should continue processing messages.
Use stop() to change it.
actor_urn
instance-attribute
The actor URN string is a universally unique identifier for the actor.
It may be used for looking up a specific actor using
ActorRegistry.get_by_urn().
__init__
Create actor.
Your are free to override __init__(), but you must call your
superclass' __init__() to ensure that the fields
actor_urn,
actor_inbox, and
actor_ref are initialized.
You can use super():
super().__init__()
Or call you superclass directly:
pykka.ThreadingActor.__init__(self)
__init__() is called before the actor is started and registered
in the ActorRegistry.
Source code in src/pykka/_actor.py
on_failure
on_failure(
exception_type: type[BaseException] | None,
exception_value: BaseException | None,
traceback: TracebackType | None,
) -> None
Run code when an unhandled exception is raised.
Hook for doing any cleanup after an unhandled exception is raised, and before the actor stops.
Threading runtime
For ThreadingActor this method is executed in
the actor's own thread, immediately before the thread exits.
The method's arguments are the relevant information from
sys.exc_info().
If an exception is raised by this method the stack trace will be logged, and the actor will stop.
Source code in src/pykka/_actor.py
on_receive
on_start
Run code at the beginning of the actor's life.
Hook for doing any setup that should be done after the actor is started, but before it starts processing messages.
Threading runtime
For ThreadingActor, this method is
executed in the actor's own thread, while __init__() is executed
in the thread that created the actor.
If an exception is raised by this method the stack trace will be logged, and the actor will stop.
Source code in src/pykka/_actor.py
on_stop
Run code at the end of the actor's life.
Hook for doing any cleanup that should be done after the actor has processed the last message, and before the actor stops.
This hook is not called when the actor stops because of an unhandled
exception. In that case, the on_failure()
hook is called instead.
Threading runtime
For ThreadingActor this method is executed
in the actor's own thread, immediately before the thread exits.
If an exception is raised by this method the stack trace will be logged, and the actor will stop.
Source code in src/pykka/_actor.py
start
classmethod
Start an actor.
Starting an actor also registers it in the
ActorRegistry.
Any arguments passed to start() will be passed on
to the class constructor.
Behind the scenes, the following is happening when you call
start():
-
The actor is created:
actor_urnis initialized with the assigned URN.actor_inboxis initialized with a new actor inbox.actor_refis initialized with apykka.ActorRefobject for safely communicating with the actor.- At this point, your
__init__()code can run.
-
The actor is registered in
pykka.ActorRegistry. -
The actor's receive loop is started.
Returns:
-
ActorRef[A]–A ref which can be used to access the actor in a safe manner.
Source code in src/pykka/_actor.py
stop
Stop the actor.
It's equivalent to calling
ActorRef.stop(block=False).
pykka.ActorRef
Bases: Generic[A]
Reference to a running actor which may safely be passed around.
ActorRef instances are returned by
Actor.start() and the lookup methods in
ActorRegistry. You should never need to create
ActorRef instances yourself.
Source code in src/pykka/_ref.py
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
ask
Send message to actor and wait for the reply.
The message can be of any type.
If block is False, it will immediately return a
Future instead of blocking.
If block is True, and timeout is None, as default, the method
will block until it gets a reply, potentially forever. If timeout is
an integer or float, the method will wait for a reply for timeout
seconds, and then raise pykka.Timeout.
Parameters:
-
message(Any) –message to send
-
block(bool, default:True) –whether to block while waiting for a reply
-
timeout(float | None, default:None) –seconds to wait before timeout if blocking
Raises:
-
Timeout–if timeout is reached if blocking
-
Exception–any exception returned by the receiving actor if blocking
Returns:
Source code in src/pykka/_ref.py
is_alive
Check if actor is alive.
This is based on the actor's stopped flag. The actor is not guaranteed
to be alive and responding even though
is_alive() returns True.
Returns:
-
bool–Trueif actor is alive,Falseotherwise.
Source code in src/pykka/_ref.py
proxy
Wrap the ActorRef in an ActorProxy.
Using this method like this:
proxy = AnActor.start().proxy()
is analogous to:
proxy = ActorProxy(AnActor.start())
Raises:
-
ActorDeadError–if actor is not available
Returns:
-
ActorProxy[A]–a proxy object wrapping the actor reference.
Source code in src/pykka/_ref.py
stop
Send a message to the actor, asking it to stop.
Returns True if actor is stopped or was being stopped at the
time of the call. False if actor was already dead. If
block is False, it returns a future wrapping the result.
Messages sent to the actor before the actor is asked to stop will be processed normally before it stops.
Messages sent to the actor after the actor is asked to stop will
be replied to with ActorDeadError after it
stops.
The actor may not be restarted.
block and timeout works as for ask().
Returns:
Source code in src/pykka/_ref.py
tell
Send message to actor without waiting for any response.
Will generally not block, but if the underlying queue is full it will block until a free slot is available.
Parameters:
-
message(Any) –message to send
Raises:
-
ActorDeadError–if actor is not available