pykka icon indicating copy to clipboard operation
pykka copied to clipboard

Akka style `Become` and `Unbecome`

Open akatashev opened this issue 4 years ago β€’ 1 comments

Based on this issue: https://github.com/jodal/pykka/issues/38

This PR adds two methods to the standard Actor class:

  1. Method _become(self, func, *args, discard_old=True) changes message handing behaviour to a specified function.
  • funcparameter 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.
  1. Method _unbecome(self) changes actor's message processing strategy to the previous one.
  • If _become was used with discard_old=True option, _unbecome switches behaviour to the standard on_receive method.
  • If _become was used with discard_old=False option and there are n message handlers in stack, behaviour will change to the handler n-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.

akatashev avatar May 19 '20 12:05 akatashev

Codecov Report

Merging #97 into develop will increase coverage by 0.26%. The diff coverage is 100.00%.

Impacted file tree graph

@@             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.

codecov[bot] avatar May 22 '20 14:05 codecov[bot]