فهرست منبع

process making the server async

digital 7 سال پیش
والد
کامیت
9164f60720
1فایلهای تغییر یافته به همراه33 افزوده شده و 23 حذف شده
  1. 33 23
      lib/beewatch/server/__init__.py

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

@@ -74,35 +74,45 @@ class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase):
         cmd,*args = data
         kwargs = {"args":args,"command":cmd,"respond":self.respond}
         func = beewatch._commands.get(cmd,False)
+        task = None
         if func == False:
             await self.respond("Unknown command")
             return
         try:
-            retval = func(**kwargs)
-            if hasattr(retval,"__await__"):
-                await curio.spawn(self.wait_for_func,retval)
-                # task = await curio.spawn(retval)
-                # task.join(0)
+            coro = func(**kwargs)
+            if hasattr(coro,"__await__"):
+                task = await coro
         except Exception as e:
-            lserver.error("Error while calling api func",exc_info=e)
+            lserver.error("api_func raised an error:",exc_info=e)
             tb = traceback.format_exc()
-            await self.respond(tb)
-
-    async def respond(self,text,*args):
-        await self.send(text)
-
-    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)
+            await self.respond(tb,log_msg="traceback of '{}'"
+                .format(e.__cause__))
+        finally:
+            pass
+        if task:
+            lserver.debug("exec: "+task.exception.__cause__)
+        # task joins iself to suppress the "task not joined" warning
+        digilib.misc.fake_wait_task()
+        cur_task = await curio.current_task()
+        await curio.ignore_after(0,cur_task.join)
+        # remove this task from running tasks list
+        digilib.network._tasks.remove(cur_task)
+
+    async def respond(self,text,*args,log_msg=False):
+        await self.send(text,log_msg)
+
+    # 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!")