1616import concurrent .futures as futures
1717import itertools
1818import logging
19+ import multiprocessing
1920import os
2021import threading
21- import multiprocessing
2222from abc import ABC , ABCMeta , abstractmethod
2323from typing import Dict , List , Type , TypeVar , Coroutine , Callable , Union , Optional
2424
@@ -473,7 +473,7 @@ async def create_actor(self, message: CreateActorMessage) -> result_message_type
473473 async def has_actor (self , message : HasActorMessage ) -> ResultMessage :
474474 result = ResultMessage (
475475 message .message_id ,
476- to_binary ( message .actor_ref .uid ) in self ._actors ,
476+ message .actor_ref .uid in self ._actors ,
477477 protocol = message .protocol ,
478478 )
479479 return result
@@ -497,7 +497,7 @@ async def destroy_actor(self, message: DestroyActorMessage) -> result_message_ty
497497 @implements (AbstractActorPool .actor_ref )
498498 async def actor_ref (self , message : ActorRefMessage ) -> result_message_type :
499499 with _ErrorProcessor (message .message_id , message .protocol ) as processor :
500- actor_id = to_binary ( message .actor_ref .uid )
500+ actor_id = message .actor_ref .uid
501501 if actor_id not in self ._actors :
502502 raise ActorNotExist (f"Actor { actor_id } does not exist" )
503503 result = ResultMessage (
@@ -649,6 +649,22 @@ async def notify_main_pool_to_destroy(
649649 ): # pragma: no cover
650650 await self .call (self ._main_address , message )
651651
652+ async def notify_main_pool_to_create (self , message : CreateActorMessage ):
653+ reg_message = ControlMessage (
654+ new_message_id (),
655+ self .external_address ,
656+ ControlMessageType .add_sub_pool_actor ,
657+ (self .external_address , message .allocate_strategy , message ),
658+ )
659+ await self .call (self ._main_address , reg_message )
660+
661+ @implements (AbstractActorPool .create_actor )
662+ async def create_actor (self , message : CreateActorMessage ) -> result_message_type :
663+ result = await super ().create_actor (message )
664+ if not message .from_main :
665+ await self .notify_main_pool_to_create (message )
666+ return result
667+
652668 @implements (AbstractActorPool .actor_ref )
653669 async def actor_ref (self , message : ActorRefMessage ) -> result_message_type :
654670 result = await super ().actor_ref (message )
@@ -775,6 +791,7 @@ async def create_actor(self, message: CreateActorMessage) -> result_message_type
775791 message .args ,
776792 message .kwargs ,
777793 allocate_strategy = new_allocate_strategy ,
794+ from_main = True ,
778795 protocol = message .protocol ,
779796 message_trace = message .message_trace ,
780797 )
@@ -952,6 +969,17 @@ async def handle_control_command(
952969 processor .result = ResultMessage (
953970 message .message_id , True , protocol = message .protocol
954971 )
972+ elif message .control_message_type == ControlMessageType .add_sub_pool_actor :
973+ address , allocate_strategy , create_message = message .content
974+ create_message .from_main = True
975+ ref = create_actor_ref (address , to_binary (create_message .actor_id ))
976+ self ._allocated_actors [address ][ref ] = (
977+ allocate_strategy ,
978+ create_message ,
979+ )
980+ processor .result = ResultMessage (
981+ message .message_id , True , protocol = message .protocol
982+ )
955983 else :
956984 processor .result = await self .call (message .address , message )
957985 return processor .result
@@ -1114,9 +1142,8 @@ def process_sub_pool_lost(self, address: str):
11141142 async def monitor_sub_pools (self ):
11151143 try :
11161144 while not self ._stopped .is_set ():
1117- for address in self .sub_processes :
1145+ for address , process in self .sub_processes . items () :
11181146 try :
1119- process = self .sub_processes [address ]
11201147 recover_events_discovered = address in self ._recover_events
11211148 if not await self .is_sub_pool_alive (
11221149 process
0 commit comments