diff --git a/htagweb/appserver.py b/htagweb/appserver.py index c7157c5..0ae7495 100644 --- a/htagweb/appserver.py +++ b/htagweb/appserver.py @@ -237,7 +237,7 @@ async def handleHome(request): return await self.serve(request,obj) self.add_route( '/', handleHome ) - async def serve(self, request, obj ) -> HTMLResponse: + async def serve(self, request, obj, force:bool=False ) -> HTMLResponse: uid = request.scope["uid"] fqn=normalize(findfqn(obj)) @@ -312,7 +312,7 @@ async def serve(self, request, obj ) -> HTMLResponse: """ % locals() - p = HrClient(uid,fqn,js,self.sesprovider.__name__) + p = HrClient(uid,fqn,js,self.sesprovider.__name__,force=force) args,kargs = commons.url2ak(str(request.url)) html=await p.start(*args,**kargs) diff --git a/htagweb/server/__init__.py b/htagweb/server/__init__.py index 84ba67a..8ee2673 100644 --- a/htagweb/server/__init__.py +++ b/htagweb/server/__init__.py @@ -50,7 +50,7 @@ def importClassFromFqn(fqn_norm:str) -> type: -def process(uid,hid,event_response,event_interact,fqn,js,init,sesprovidername): +def process(uid,hid,event_response,event_interact,fqn,js,init,sesprovidername,force): #'''''''''''''''''''''''''''''''''''''''''''''''''''' if sesprovidername is None: sesprovidername="MemDict" @@ -107,6 +107,7 @@ async def update(actions): if params.get("cmd") == CMD_RENDER: # just a false start, just need the current render print(f">Process {pid} render {hid}") + hr.session = FactorySession(uid) # reload session assert await bus.publish(event_response,str(hr)) else: print(f">Process {pid} interact {hid}:") @@ -169,18 +170,20 @@ def killall(ps:dict): if hid in ps and ps[hid]["process"].is_alive(): # process is already running - if key_init == ps[hid]["key"]: + if params["force"] or key_init != ps[hid]["key"]: + # kill itself because it's not the same init params, or force recreate + if params["force"]: + print("Recreate a new process (forced)",hid) + else: + print("Recreate a new process (qp changed)",hid) + ps[hid]["process"].kill() + # and recreate another one later + else: # it's the same initialization process # so ask process to send back its render assert await bus.publish(params["event_interact"],dict(cmd=CMD_RENDER)) continue - else: - # kill itself because it's not the same init params - print("Reload a new process",hid) - ps[hid]["process"].kill() - - # and recreate another one later # create the process p=multiprocessing.Process(target=process, args=[],kwargs=params) diff --git a/htagweb/server/client.py b/htagweb/server/client.py index b7dff15..bec73df 100644 --- a/htagweb/server/client.py +++ b/htagweb/server/client.py @@ -15,13 +15,14 @@ TIMEOUT=20 # sec to wait answer from redys server #TODO: set better class HrClient: - def __init__(self,uid:str,fqn:str,js:str=None,sesprovidername=None): + def __init__(self,uid:str,fqn:str,js:str=None,sesprovidername=None,force=False): """ !!!!!!!!!!!!!!!!!!!! if js|sesprovidername is None : can't do a start() !!!!!!!!!!!!!!!!!!!!!!""" self.uid=uid self.fqn=fqn self.js=js self.bus = redys.v2.AClient() self.sesprovidername=sesprovidername + self.force=force self.hid=f"{uid}_{fqn}" self.event_response = f"response_{self.hid}" @@ -56,6 +57,7 @@ async def start(self,*a,**k) -> str: js=self.js, init= (a,k), sesprovidername=self.sesprovidername, + force=self.force, )) # wait 1st rendering diff --git a/test_server.py b/test_server.py index 648a52d..f30953f 100644 --- a/test_server.py +++ b/test_server.py @@ -30,6 +30,10 @@ async def test_base( server ): html=await p.start() assert html.startswith("") + p=HrClient(uid,"test_hr:App","//",force=True) + html=await p.start() + assert html.startswith("") + actions=await p.interact( oid="ut", method_name="doit", args=[], kargs={}, event={} ) assert "update" in actions