Weird ActorTypeDispatcher behavior: bug or doc update?

13 views
Skip to first unread message

Todd Cook

unread,
Feb 16, 2021, 1:09:53 PM2/16/21
to thespian.py
I was fooling around with debugging some Actors and stumbled across this weird behavior. I'm not sure if it's a bug or maybe just need an update of the docs. If an actor inherits from ActorTypeDispatcher and then at a later time a regular receiveMessage handler is added, then the typed receives will no longer be called, e.g.

```
% ipython
Python 3.8.5 (default, Sep  4 2020, 02:22:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:
   ...: import logging
   ...: from thespian.actors import ActorTypeDispatcher, ActorSystem
   ...:
   ...: class Dummy(ActorTypeDispatcher):
   ...:     def receiveMsg_str(self, message, sender):
   ...:         logging.warning("Dummy got string")
   ...:
   ...: class RealDummy(ActorTypeDispatcher):
   ...:     def receiveMessage(self, message, sender):
   ...:         if isinstance(message, str):
   ...:             logging.warning("Real Dummy got string but didn't expect one!")
   ...:
   ...:     def receiveMsg_str(self, message, sender):
   ...:         logging.warning("Got string")
   ...:
   ...: asys = ActorSystem()
   ...: dummy = asys.createActor(Dummy)
   ...: real_dummy = asys.createActor(RealDummy)
   ...:
   ...: asys.tell(dummy, "hello")
   ...: asys.tell(real_dummy, "hello")
   ...:
2021-02-16 10:06:25,799 WARNING =>  Dummy got string  [<ipython-input-1-1c181ae5a412>:6]
2021-02-16 10:06:25,799 WARNING =>  Real Dummy got string but didn't expect one!  [<ipython-input-1-1c181ae5a412>:11]
```
Of course, now I see one shouldn't do this, but the behavior was a little surprising, no?

Kevin Quick

unread,
Feb 16, 2021, 1:20:02 PM2/16/21
to Todd Cook, thespian.py
Thanks for calling this out, Todd.  

This should be documented: the `ActorTypeDispatcher` is essentially a base class that provides the `receiveMessage` in the base class and then dispatches on message type to the subclass's methods, so if the subclass overrides `receiveMessage` then that replaces the base class's functionality.  The current documentation doesn't clarify this base class scenario, and it should.

Also note that there is an Actorize.py in the contrib directory that provides an alternate way of doing message-type dispatching (using decorators).  It also replaces the `receiveMessage`, but contains protection code to generate an assertion error if you try to Actorize a class that defines its own `receiveMessage` method.

Regards,
  Kevin




--
You received this message because you are subscribed to the Google Groups "thespian.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thespianpy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/thespianpy/6fee1b35-8b64-4e7c-9221-19d0940b1768n%40googlegroups.com.


--
-KQ

Todd Cook

unread,
Feb 16, 2021, 1:23:00 PM2/16/21
to thespian.py
Thank you for explanation and the tip about Actorize!
Reply all
Reply to author
Forward
0 new messages