Python Forum
asyncio stops responding to requests
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asyncio stops responding to requests
#2
I found the solution to my problem. I needed to add a callback function for the two tasks, and return values for each Task.

Otherwise, when an exception is raised in a Task, it will not be seen until the Task goes out-of-scope, which in my case since
the Python server script is running in a run_forever loop, will never go out-of-scope until the server script receives a SIGINT.

In case anyone else has a similar problem, here's the update code snippet:

import asyncio
import socket
.....
 
addr = (localhost, 50000)
 
loop = asyncio.get_event_loop()
 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(False)
sock.bind(addr)
sock.listen(N)
 
def _handle_ahandle_throws(task: asyncio.Task):
      try:
        task.result()
      except asyncio.CancelledError:
        pass
      except Exception:
        logging.exception('Exception raised in ahandle...by task = %r', task)

async def sserver():
     while True:
          conn, addr = await loop.sock_accept(sock)
          sr = SRequests(pool, loop, log)
          sums_task = loop.create_task(sr.ahandle(conn))
          sums_task.add_done_callback(_handle_ahandle_throws)
 
loop.create_task(sserver())
 
try:
    loop.run_forever()
except KeyboardInterrupt:
    sserver.close()
    loop.run_until_complete(sserver.wait_close())
    loop.close()
    sock.shutdown(socket.SHUT_RDWR)
    sock.close()
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()
    sock.shutdown(socket.SHUT_RDWR)
    sock.close()

...

    class SRequests(asyncio.Protocol, SRequest)
         def __init__(self, pool, loop, log):
              self.pool = pool
              self.loop = loop
              self.log = log
              self.setup()
              return None

         def _handle_db_task_throws(task: asyncio.Task):
              try:
                  task.result()
              except asyncio.CancelledError:
                  pass
              except Exception:
                 logging.exception('Exception raised by db_task = %r', task)

          async def getDBConn(self):
                ...
                dbConn = DBConnection(...)
                ...
                return dbConn

          def setup(self):
                ...
                db_task = self.loop.create_task(self.getDBConn())
                db_task.add_done_callback(_handle_db_task_throws)
                ...
Reply


Messages In This Thread
asyncio stops responding to requests - by mansky - Jul-27-2021, 05:48 PM
RE: asyncio stops responding to requests - by mansky - Aug-30-2021, 10:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Configuring requests module to use secondary IP on server for outbound requests mohit 1 6,567 Oct-24-2016, 05:21 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020