Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Based on this issue: #38
This PR adds two methods to the standard
Actor
class:_become(self, func, *args, discard_old=True)
changes message handing behaviour to a specified function.func
parameter is a callable function that should be executed on message receive.*args
parameter allows us to pass any values we need to the function and to avoid using "mutable" class properties to keep actor state (surely, there is no immutability in Python, but still class properties are not used).discard_old=True
parameter says whether it should to replace actual behaviour with this new handler completely or it should stash handlers in a handlers stack to be able to return back to them._unbecome(self)
changes actor's message processing strategy to the previous one._become
was used withdiscard_old=True
option,_unbecome
switches behaviour to the standardon_receive
method._become
was used withdiscard_old=False
option and there aren
message handlers in stack, behaviour will change to the handlern-1
._become
wasn't used, and actual behaviour is the default one, nothing happens.These methods allow to model an FSM with an actor, using
_become
and_unbecome
to switch between states.