Pārlūkot izejas kodu

error_handler now runs async methods properly, trying to figure out what's up with joining tasks

digital 7 gadi atpakaļ
vecāks
revīzija
3ca205d781
1 mainītis faili ar 12 papildinājumiem un 7 dzēšanām
  1. 12 7
      network/__init__.py

+ 12 - 7
network/__init__.py

@@ -290,25 +290,29 @@ class Server(object):
         it unregisters itself from atexit, so it doesn't get executed twice
         it unregisters itself from atexit, so it doesn't get executed twice
         when it was manually called before to program exits.
         when it was manually called before to program exits.
         """
         """
-        def error_handler(func,*args,log_text="error",async_=False,**kwargs):
+        async def error_handler(
+            func,*args,log_text="error",async_=False,**kwargs):
             try:
             try:
                 if async_:
                 if async_:
-                    coro = func(*args,**kwargs)
-                    lserver.debug("{} {}".format(func,coro))
+                    await func(*args,**kwargs)
                 else:
                 else:
                     func(*args,**kwargs)
                     func(*args,**kwargs)
             except Exception as exc:
             except Exception as exc:
                 lserver.debug("error occured during "+log_text,exc_info=exc)
                 lserver.debug("error occured during "+log_text,exc_info=exc)
         atexit.unregister(self.shutdown)
         atexit.unregister(self.shutdown)
         lserver.info("shutting down server")
         lserver.info("shutting down server")
-        error_handler(self.handle_tasks.cancel_remaining,
+        await error_handler(self.handle_tasks.cancel_remaining,
             log_text="handler cancel",async_=True)
             log_text="handler cancel",async_=True)
+        try:
+            await curio.ignore_after(0.01,self.handle_tasks.join)
+        except Exception as exc:
+            lserver.error("error joining tasks:",exc_info=exc)
         # check if there is actually a socket. if the shutdown method is
         # check if there is actually a socket. if the shutdown method is
         # executed before the start method, there is no socket.
         # executed before the start method, there is no socket.
         if self.socket:
         if self.socket:
-            error_handler(self.socket.shutdown,socket.SHUT_RDWR,
+            await error_handler(self.socket.shutdown,socket.SHUT_RDWR,
                 log_text="socket shutdown")
                 log_text="socket shutdown")
-            error_handler(self.socket.close,log_text="socket close")
+            await error_handler(self.socket.close,log_text="socket close")
 
 
     def start(self):
     def start(self):
         """
         """
@@ -381,8 +385,9 @@ class Server(object):
         # seconds. instead of task.join() we use task.wait(). the only
         # seconds. instead of task.join() we use task.wait(). the only
         # difference is that wait doesn't throw a exception if the task was
         # difference is that wait doesn't throw a exception if the task was
         # stopped or crashed
         # stopped or crashed
+        # maybe wait doesn't work, trying it out with join
         cur_task = await curio.current_task()
         cur_task = await curio.current_task()
-        await curio.ignore_after(0,cur_task.wait)
+        await curio.ignore_after(0.01,cur_task.join)
 
 
 class Client(threading.Thread):
 class Client(threading.Thread):
     """docstring for Client"""
     """docstring for Client"""