Created
September 4, 2017 16:53
-
-
Save vidbina/22f6a759b934b88e7bf0e16907109309 to your computer and use it in GitHub Desktop.
Playing w/ DeadLetter and ChildActorExited msgs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from datetime import timedelta | |
from thespian.actors import * | |
# The thing is that a A1 is only truly the parent of A2 if it spawns A2. | |
# In the former implementation A1 and A2 were all spawned by the application | |
# root node and as such only reported their exit status to that node. | |
# The current approach basically sends the class to the A1 actor and has the | |
# A1 actor spawn A2 | |
# Another approach would be to subsribe A1 to handle A2's ChildActorExited | |
# messages. In that case A1 is not the parent (the root node still is), but | |
# A1 could be the supervisor -- a concept familiar in other actor frameworks | |
# but one I still have to figure out in this one ;) | |
class A1(Actor): | |
def receiveMessage(self, msg, sender): | |
if isinstance(msg, tuple): | |
print(f'A1: Init') | |
self.a2_type = msg[1] | |
self.a2 = self.createActor(self.a2_type) | |
self.handleDeadLetters(True) | |
self.send(self.a2, 1) | |
elif msg == 2: | |
print(f'A1: Got message 2 from A2') | |
self.got = False | |
self.wakeupAfter(timedelta(seconds=1)) | |
elif msg == 3: | |
self.got = True | |
elif isinstance(msg, WakeupMessage): | |
print(f'A1: Woke up. Got {self.got}.') | |
if not self.got: | |
self.send(self.a2, 3) | |
self.wakeupAfter(timedelta(seconds=1)) | |
elif isinstance(msg, DeadEnvelope): | |
print(f'A1: Got DeadEnvelope') | |
elif isinstance(msg, ChildActorExited): | |
print(f'Shit, my baby just died. Take action!!!') | |
else: | |
print(f'A1: Got unkownn {type(msg)}') | |
class A2(Actor): | |
def receiveMessage(self, msg, sender): | |
if msg == 1: | |
print(f'A2: Init') | |
self.send(sender, 2) | |
self.send(self.myAddress, ActorExitRequest()) | |
if msg == 3: | |
print(f'A2: got message 3 from A1') | |
self.send(sender, 3) | |
if __name__ == '__main__': | |
asys = ActorSystem('multiprocTCPBase') | |
#a2 = asys.createActor(A2) | |
a1 = asys.createActor(A1) | |
asys.tell(a1, (0, A2)) | |
try: | |
while True: | |
asys.listen(0.5) | |
except KeyboardInterrupt: | |
asys.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment