tractor
tractor copied to clipboard
Actor state via messages
An example for @fjarri doing "actor state mutation via messages".
Feel free to criticize the heck out of it.
If you want to see how subactors could modify each other's state we can add that as well.
How's that 2nd example @fjarri for a class / ray style actors api?
Sorry, there's been a lot of input from you in different channels, I'll summarize my thoughts here.
I'm afraid I still don't see the functionality (or even ways to implement the functionality) I need. When I think of an actor system, these are some of things I'm worried about:
- how can a child send a message to its parent?
- if a child wants to terminate or restart itself, how will the parent handle it?
- how can I make message handlers async? How can I guarantee that as long as one message handler of an actor is running, another one will not be started?
- how can I start a child in the same thread/other thread/other process with the same API, without changing the child's code?
- how can I delay the message delivery?
- how can I monitor the state of an actor from another actor (not necessarily the parent)?
I can see how my API (in https://gist.github.com/fjarri/803d2611fc8487c7853f17ed4ad1ec10) allows one to implement this functionality. The framework will have enough information to handle these problems. Right now tractor seems too low-level for all that. I will have no more help from it than I do from trio itself, except for more convenient multiprocessing.
@fjarri thanks so much for the writeup 👍🏼.
I'll address these in order:
how can a child send a message to its paren
async with tractor.wait_for_actor('parent_name') as portal:
... do stuff with portal...
This is documented only very briefly. Note the "arbiter" stuff there is wrong and going away.
if a child wants to terminate or restart itself, how will the parent handle it?
However it wants to. Restarts are a naive basic operation the exact same as spawning or running a task through a portal. If you want a special error that the parent expects to handle then you can just raise it and expect it on the parent's side or you could just have the result of the remote task return a restart message of your choosing. I've pushed up an example showing both multiple task(s) restarts within a single subactor as well as restarts of multiple subactors entirely themselves: https://github.com/goodboy/tractor/pull/190/files#diff-3d37ffe6c0c1d6567affe87d7c0edca4c18adb1d5735075084263351e1f642f8
To understand what's going more easily from a terminal you can run it with the following command:
$TERM -e watch -n 0.1 "pstree -a $$" & python examples/actors/most_basic_supervisor.py || kill $!
how can I start a child in the same thread/other thread/other process with the same API, without changing the child's code?
I honestly have no idea what you're asking here so you'll have to clarify. There is currently no hot-reloading of code support yet mostly because python doesn't have great facilities for this and thus it needs some thinking but we've had lots of discussion.
how can I delay the message delivery?
Again don't know what you mean. If you want a reactive style delay there are already trio libs underway supporting this.
how can I monitor the state of an actor from another actor (not necessarily the parent)?
As per the 1st bullet, contact the actor by name, get a portal, send it a message, get a response. There is no built-in "monitoring" system on purpose for now.
FYI we can probably vastly improve the example "proxy" code here with the new native bidir streaming support in #209 🏄🏼