pykka
pykka copied to clipboard
Supervision
Currently, the only way to know that the "child" actor failed, is to employ the 'ask' pattern:
actor = MyActor.start()
try:
actor.ask({'command':'hey'})
except Exception:
# actor failed!
However, if we are using tell, the is no way to "detect" a failure:
actor.tell({'command':'throw me an exception!'})
# Everything is great!
Akka handles this by registering an actor for "death watch": http://doc.akka.io/docs/akka/snapshot/general/supervision.html
In pykka we can implement a similar thing manually by providing an actor with a parent
argument and overriding on_failure
in the MyActor
:
class MyActor(ThreadingActor):
def __init__(self, parent):
super(MyActor, self).__init__()
self.parent = parent
def on_failure(self, exception_type, exception_value, traceback):
self.parent.tell({'command': 'death', 'exception': exception_value})
Then the parent actor would simply have to handle it, like so:
class Parent(ThreadingActor):
def __init__(self, parent):
super(Parent, self).__init__()
self.child = MyActor.start(parent=self.actor_ref)
def on_receive(self, message):
if message.get('command') == 'death:
# Child actor died....
if not self.child.is_alive():
# restart it...
self.child = MyActor.start(parent=self.actor_ref)
P.S.: On the readme page, where it says what "pykka is not" --- supervision is one of the mentions. Is there any specific reasons (aside from time/effort) that its not? I.e. are you opposed to the idea?
Thanks for providing a good howto on how to implement a supervision pattern yourself. Supervision is mostly lacking from Pykka because of time/effort/need, and not because I oppose it. I'm interested in including it in a future Pykka 2.
I'll leave this bug open to either add the pattern to docs or to use it as basis for some future supervision support