def __init__(self, EnvId, CorpsMgrQueue, ConfigDelta): #from time import perf_counter #t1 = perf_counter() apply_config_delta('ConfigGlobals', ConfigDelta) from ConfigGlobals import Logging_Format, Logging_Datefmt, Logging_Level basicConfig(format=Logging_Format, datefmt=Logging_Datefmt, level=Logging_Level) # Self-initialize ConcId = _ConcIdMgr.new() assert ConcId == ENVMGR_CONCID, f'EnvMgr in Env {EnvId} has ConcId {ConcId} and not {ENVMGR_CONCID}' self.ConcAddr = ConcAddr(CORPSMGR_ENVID, ConcId, EnvId) self.EnvId = EnvId set_EnvStatus(MajorStatus.Booting) super().__init__() # Init some EnvGlobals set_EnvId(EnvId) # Make sure thread-local data knows the Conc it's assigned to TheThread.TheConcAddr = self.ConcAddr # Make sure we can be found _Addr2Conc.register(self, self.ConcAddr) # Startup the TcpConnector and get our port number from it TheEnvRecord = copy(DefaultEnvRecord) EnvQueue = queue.Queue() TheEnvRecord.NetwFactory.new_connector(TheEnvRecord.IpAddr, EnvQueue) Port = EnvQueue.get() # Initialize our EnvRecord and register it in the EnvTable TheEnvRecord.Port = Port set_MyPort(Port) # another EnvGlobal _EnvTable.register(EnvId, TheEnvRecord) # If we are not the CorpsMgr's Env we need to provide the CorpsMgr our XferEnvRecord if EnvId != CORPSMGR_ENVID: OurXferEnvRecord = XferEnvRecord(EnvId, TheEnvRecord.IpAddr, TheEnvRecord.Port) CorpsMgrQueue.put(OurXferEnvRecord) # The thread is no longer assigned, so cleanup TheThread.TheConcAddr = NullConcAddr set_EnvStatus(MajorStatus.Running) info(f'{self} initialized')
def __init__(self, EnvId, CorpsMgrQueue, ConfigFiles): load_config('ConfigGlobals', ConfigFiles) from ConfigGlobals import Logging_Format, Logging_Datefmt, Logging_Level basicConfig(format=Logging_Format, datefmt=Logging_Datefmt, level=Logging_Level) # Self-initialize ConcId = _ConcIdMgr.new() assert ConcId == ENVMGR_CONCID, f'EnvMgr in Env {EnvId} has ConcId {ConcId} and not {ENVMGR_CONCID}' self.ConcAddr = ConcAddr(CORPSMGR_ENVID, ConcId, EnvId) self.EnvId = EnvId super().__init__() # Init some EnvGlobals set_EnvId(EnvId) set_EnvMgrAddr(self.ConcAddr) # Make sure thread-local data knows the Conc it's assigned to TheThread.TheConcAddr = self.ConcAddr # Make sure we can be found _Addr2Conc.register(self, self.ConcAddr) # Startup the TcpConnector and get our port number from it TheEnvRecord = copy(DefaultEnvRecord) EnvQueue = queue.Queue() TheEnvRecord.NetwFactory.new_connector(TheEnvRecord.IPAddr, EnvQueue) Port = EnvQueue.get() # Initialize our EnvRecord and register it in the EnvTable TheEnvRecord.Port = Port set_MyPort(Port) # another EnvGlobal _EnvTable.register(EnvId, TheEnvRecord) # If we are not the CorpsMgr's Env we need to provide the CorpsMgr our # EnvRecord Spec Tuple: 0: LocEnvId, 1: IPAddr, 2: Port if EnvId != CORPSMGR_ENVID: CorpsMgrQueue.put([EnvId, TheEnvRecord.IPAddr, TheEnvRecord.Port]) # The thread is no longer assigned, so cleanup TheThread.TheConcAddr = NullConcAddr info(f'EnvMgr {self.my_Name()} Env {EnvId} initialized')
def init_EnvTable(self, XerEnvRecordList): ''' Init our EnvTable using a list of XferEnvRecords Ignore for our own which we did in __init__ For now use defaults for MsgHdlrFactory and NetwHdlrFactory ''' for anXferEnvRecord in XerEnvRecordList: EnvId = anXferEnvRecord.LocEnvId if EnvId != my_EnvId(): TheEnvRecord = copy(DefaultEnvRecord) TheEnvRecord.IpAddr = anXferEnvRecord.IpAddr TheEnvRecord.Port = anXferEnvRecord.Port _EnvTable.register(EnvId, TheEnvRecord) info(f'{self} EnvTable:\n{_EnvTable}') return True
def init_EnvTable(self, AllEnvMsgList): ''' Init our EnvTable using a list of EnvRecord Spec Tuples: 0: LocEnvId, 1: IPAddr, 2: Port Ignore for our own which we did in __init__ For now use defaults for MsgHdlrFactory and NetwHdlrFactory ''' for EnvMsgList in AllEnvMsgList: # 0: LocEnvId, 1: IPAddr, 2: Port EnvId = EnvMsgList[0] if EnvId != my_EnvId(): TheEnvRecord = copy(DefaultEnvRecord) TheEnvRecord.IPAddr = EnvMsgList[1] TheEnvRecord.Port = EnvMsgList[2] _EnvTable.register(EnvId, TheEnvRecord) info( f'EnvMgr {self.my_Name()} Env {my_EnvId()} EnvTable:\n{_EnvTable}') return True
def __init__(self, ConfigFiles): # Import the Config data here to make sure we get the version loaded by EnvMgr from ConfigGlobals import NumEnvs assert NumEnvs < MAX_ENVID - MIN_ENVID + 1, f'Number of Envs must be less than {MAX_ENVID-MIN_ENVID+1}' # Self-initialize ConcId = _ConcIdMgr.new() assert ConcId == CORPSMGR_CONCID, f'CorpsMgr has ConcId {ConcId} and not {CORPSMGR_CONCID}' self.ConcAddr = ConcAddr(CORPSMGR_ENVID, ConcId, CORPSMGR_ENVID) super().__init__() # Make sure thread-local data knows the Conc it's assigned to TheThread.TheConcAddr = self.ConcAddr # Make sure we can be found _Addr2Conc.register(self, self.ConcAddr) # Build a list of Envs' list of EnvRecord Spec Tuple: 0: LocEnvId, 1: IPAddr, 2: Port # Ours first, the other Envs as we receive them off the Queue EnvRecordSpecs = [] OurEnvRecord = _EnvTable.get(CORPSMGR_ENVID) EnvRecordSpecs.append( [CORPSMGR_ENVID, OurEnvRecord.IPAddr, OurEnvRecord.Port]) # Boot all other Envs in this Corps and get their EnvRecord Spec # ...Everything is on this Host CorpsMgrQueue = Queue() for i in range(NumEnvs - 1): NewEnvId = i + MIN_ENVID + 1 NewEnv = Process(target=Env, args=(NewEnvId, CorpsMgrQueue, ConfigFiles)) NewEnv.start() EnvMsgList = CorpsMgrQueue.get() EnvRecordSpecs.append(EnvMsgList) # Create an EnvRecord and add to our EnvTable NewEnvRecord = EnvRecord(DefaultEnvRecord.MsgHdlrFactory, DefaultEnvRecord.NetwFactory, EnvMsgList[1], EnvMsgList[2]) _EnvTable.register(NewEnvId, NewEnvRecord) info(f'CorpsMgr {self.my_Name()} EnvTable:\n{_EnvTable}') # Send EnvRecordSpecs to all Envs except ours for i in range(NumEnvs - 1): RemoteEnvId = i + MIN_ENVID + 1 EnvConcAddr = ConcAddr(CORPSMGR_ENVID, ENVMGR_CONCID, RemoteEnvId) RemoteEnv = EnvName(EnvConcAddr) FutRes = RemoteEnv.init_EnvTable(EnvRecordSpecs) Ret = FutRes.Ret assert Ret == True,\ f'CorpsMgr {self.my_Name()} Process {getpid()} error initing EnvTable in Env {RemoteEnvId}' info(f'CorpsMgr {self.my_Name()} initialized, starting...') # The thread is no longer assigned, so cleanup TheThread.TheConcAddr = NullConcAddr
def __init__(self, ConfigDelta): ''' Init a Corps, including all of its initial Envs (except its own - Corps Conc has already booted Env 0). ''' # Import the Config data here to make sure we get the version loaded by EnvMgr from ConfigGlobals import NumEnvs print(f'{self} initing with {NumEnvs} Envs') assert NumEnvs < MAX_ENVID-MIN_ENVID+1, f'Number of Envs must be less than {MAX_ENVID-MIN_ENVID+1}' # Self-initialize ConcId = _ConcIdMgr.new() assert ConcId == CORPSMGR_CONCID, f'CorpsMgr has ConcId {ConcId} and not {CORPSMGR_CONCID}' self.ConcAddr = ConcAddr(CORPSMGR_ENVID, ConcId, CORPSMGR_ENVID) super().__init__() self.MajorStatus = MajorStatus.Booting # Make sure thread-local data knows the Conc it's assigned to TheThread.TheConcAddr = self.ConcAddr # Make sure we can be found _Addr2Conc.register(self, self.ConcAddr) # todo: make tuple into a named tuple or a data class? # Build a list of Envs' list of EnvRecord specs with indexes: 0: LocEnvId, 1: IpAddr, 2: Port # Ours first, the other Envs as we receive them off the Queue XferEnvRecords = [] OurEnvRecord = _EnvTable.get(CORPSMGR_ENVID) OurXferEnvRecord = XferEnvRecord(CORPSMGR_ENVID, OurEnvRecord.IpAddr, OurEnvRecord.Port) XferEnvRecords.append(OurXferEnvRecord) # Boot all other Envs in this Corps and get their EnvRecord Spec # ...Everything is on this Host #from time import perf_counter EnvIds = EnvIdGen(MIN_ENVID_plus_1, NumEnvs-1) #tot_t = 0 for NewEnvId in EnvIds: #t1 = perf_counter() CorpsMgrQueue = Queue() NewEnv = Process(target=Env, args=(NewEnvId, CorpsMgrQueue, ConfigDelta)) NewEnv.start() NewXferEnvRecord = CorpsMgrQueue.get() #t2 = perf_counter() #tot_t += (t2 - t1) #print(f'CorpsMgr init Env {NewEnvId} in {t2-t1} seconds') assert NewXferEnvRecord.LocEnvId == NewEnvId, \ f'CorpsMgr __init__ returned Envid {NewXferEnvRecord.LocEnvId} vs expected {NewEnvId}' XferEnvRecords.append(NewXferEnvRecord) # Create an EnvRecord and add to our EnvTable NewEnvRecord = EnvRecord(DefaultEnvRecord.MsgHdlrFactory, DefaultEnvRecord.NetwFactory, \ NewXferEnvRecord.IpAddr, NewXferEnvRecord.Port) _EnvTable.register(NewEnvId, NewEnvRecord) #print(f'CorpsMgr {self.ConcAddr} init: queue get {NumEnvs-1} Env processes in {tot_t} seconds') info(f'{self} EnvTable:\n{_EnvTable}') # Send EnvRecordSpecs to all Envs except ours EnvNames = EnvNameGen(MIN_ENVID_plus_1, NumEnvs-1) FutRets = [] for RemoteEnv in EnvNames: FutRets.append(RemoteEnv.init_EnvTable(XferEnvRecords)) # todo: handle the exception for FutRet in FutRets: Ret = FutRet.Ret # will blow up if we have an exception # We're up! self.MajorStatus = MajorStatus.Running info(f'{self} initialized, starting...') # The thread is no longer assigned, so cleanup TheThread.TheConcAddr = NullConcAddr