-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initialising an actor system with cyclic references #30
Comments
I think I'd just send the last reference as a message to the actor needing it after they're started. You could also look up the last reference in the actor registry, though I tend to try to avoid using the registry since it is shared state. Note that if you're using proxies and have a cyclic graph of actors, you can quickly fall into the trap of creating a deadlock if you regularly block on method calls between these actors. I've fallen into that pit myself, and had to use |
Good insight... I guess the 'references' I speak of need really be Right now I create a bag of actor proxies and quickly start the actors in quick succession, assigning them fields of the bag. Thankfully, I'm only interacting with other actors while handling messages atm, but I can see an issue such as this: class ActorSystem:
pass
system = ActorSystem()
system.a = FooActor.start(system)
system.b = BarActor.start(system)
system.c = BazActor.start(system)
system.d = QuuxActor.start(system)
class FooActor:
def __init__(self, system):
# call super...
self.system = system
def on_start(self):
self.system.b.tell({"Cool": "beans"}) # this would be a problem: this may execute before the assignment onto system.b will have happened
...
# similar declarations for b, c, d Obviously initialisation order is horrible terrible here, but I don't see any other way with the current API. This prompted me to type in "Dependency Injection Python" into Google. This doesn't bode well! An idea: make the |
There is no reason you should be passing the whole system around. What are you trying to accomplish that requires circular references? Actor systems should be organized in supervision-hierarchies (something currently missing from pykka unfortunately): Here is an example of a hierarchy known as Error Kernel, where dangerous behavior is pushed down to the leaves: If you need a ring of actors that pass messages to each other, consider using an intermediary "bus" actor, that will deliver the messages instead. Or, alternatively you can create a ring network -- in which case you need an actor that will actually DO the connecting, i.e.:
Forgive the silly example. |
I've since moved on from that particular project. You raise very valid points. Closing. |
Pykka's API (
.start
method) doesn't really let the user make actor systems with cyclic referential relationships like:where
a -> b
denotes that actora
holds a reference to actorb
(probably for the better!)However, I find this useful.
What is idiomatic Pykka for initialising such a ring of actors?
The text was updated successfully, but these errors were encountered: