pykka
pykka copied to clipboard
Akka style `Become` and `Unbecome`
Based on this issue: https://github.com/jodal/pykka/issues/38
This PR adds two methods to the standard Actor
class:
- Method
_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.
- Method
_unbecome(self)
changes actor's message processing strategy to the previous one.
- If
_become
was used withdiscard_old=True
option,_unbecome
switches behaviour to the standardon_receive
method. - If
_become
was used withdiscard_old=False
option and there aren
message handlers in stack, behaviour will change to the handlern-1
. - If
_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.
Codecov Report
Merging #97 into develop will increase coverage by
0.26%
. The diff coverage is100.00%
.
@@ Coverage Diff @@
## develop #97 +/- ##
===========================================
+ Coverage 95.37% 95.63% +0.26%
===========================================
Files 13 13
Lines 562 573 +11
===========================================
+ Hits 536 548 +12
+ Misses 26 25 -1
Impacted Files | Coverage Ξ | |
---|---|---|
pykka/_actor.py | 97.03% <100.00%> (+1.06%) |
:arrow_up: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Ξ = absolute <relative> (impact)
,ΓΈ = not affected
,? = missing data
Powered by Codecov. Last update 4c4c9a7...deda7f7. Read the comment docs.