1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from typing import Any , Dict , Type , Tuple
16+
1517from .context import get_context
18+ from .core import _Actor
1619
1720
1821async def create_actor (actor_cls , * args , uid = None , address = None , ** kwargs ):
@@ -33,3 +36,48 @@ async def destroy_actor(actor_ref):
3336def actor_ref (* args , ** kwargs ):
3437 ctx = get_context ()
3538 return ctx .actor_ref (* args , ** kwargs )
39+
40+
41+ class Actor (_Actor ):
42+ def __new__ (cls , * args , ** kwargs ):
43+ try :
44+ return _actor_implementation [cls ](* args , ** kwargs )
45+ except KeyError :
46+ return super ().__new__ (cls , * args , ** kwargs )
47+
48+ async def __post_create__ (self ):
49+ """
50+ Method called after actor creation
51+ """
52+ return await super ().__post_create__ ()
53+
54+ async def __pre_destroy__ (self ):
55+ """
56+ Method called before actor destroy
57+ """
58+ return await super ().__pre_destroy__ ()
59+
60+ async def __on_receive__ (self , message : Tuple [Any ]):
61+ """
62+ Handle message from other actors and dispatch them to user methods
63+
64+ Parameters
65+ ----------
66+ message : tuple
67+ Message shall be (method_name,) + args + (kwargs,)
68+ """
69+ return await super ().__on_receive__ (message )
70+
71+
72+ _actor_implementation : Dict [Type [Actor ], Type [Actor ]] = dict ()
73+
74+
75+ def register_actor_implementation (actor_cls : Type [Actor ], impl_cls : Type [Actor ]):
76+ _actor_implementation [actor_cls ] = impl_cls
77+
78+
79+ def unregister_actor_implementation (actor_cls : Type [Actor ]):
80+ try :
81+ del _actor_implementation [actor_cls ]
82+ except KeyError :
83+ pass
0 commit comments