Browse Source

bug fixing

digital 7 years ago
parent
commit
4ed444a291
1 changed files with 23 additions and 10 deletions
  1. 23 10
      lib/beewatch/server/__init__.py

+ 23 - 10
lib/beewatch/server/__init__.py

@@ -68,31 +68,44 @@ class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
             "turn":self.engines_ctrl.turn
         }
 
-    def handle(self, data):
+    async def handle(self, data):
         data = data.strip()
         data = data.split(" ")
         cmd,*args = data
         kwargs = {"args":args,"command":cmd,"respond":self.respond}
         func = beewatch._commands.get(cmd,False)
         if func == False:
-            self.respond("Unknown command")
+            await self.respond("Unknown command")
             return
         try:
             retval = func(**kwargs)
             if hasattr(retval,"__await__"):
-                lserver.debug("curio.spawn started")
-                task = curio.spawn(retval)
-                lserver.debug("curio.spawn finished")
+                await curio.spawn(self.wait_for_func,retval)
+                # task = await curio.spawn(retval)
+                # task.join(0)
         except Exception as e:
             lserver.error("Error while calling api func",exc_info=e)
             tb = traceback.format_exc()
-            self.respond(tb)
+            await self.respond(tb)
 
-    def respond(self,text,*args):
-        self.send(text)
+    async def respond(self,text,*args):
+        await self.send(text)
 
-    def welcome_client(self):
-        self.send("this is the server speaking, hello new client!")
+    async def wait_for_func(self,coro):
+        try:
+            task = await curio.spawn(coro)
+            try:
+                await task.join()
+            except Exception as e:
+                lserver.error("error during api_func execution",exc_info=e)
+            # we do this so we don't get an "task was not joined" error
+            cur_task = await curio.current_task()
+            await curio.ignore_after(0,cur_task.join)
+        except Exception as e:
+            lserver.error("error",exc_info=e)
+
+    async def welcome_client(self):
+        await self.send("this is the server speaking, hello new client!")