2

So this is some code i have in my python file, the details of the code aren't that important, I basically use this MainHandler to deal with some requests sent to it by a connection "client".

class MainHandler(tornado.web.RequestHandler):
   #some code
   async def connect(self):
      client = self.request.connection.stream
      #some code
      data = await client.read_bytes(10000, partial = True)

My doubt is if there's a way of waiting for the client.read_bytes() while a condition is still met. When that condition fails, I need to stop the "await".

am.torrinha
  • 59
  • 1
  • 8
  • So you want to cancel the `await` if something in particular happens? You probably need to [`cancel`](https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.cancel) the task somehow. – jdehesa Apr 18 '19 at 14:22
  • actually, I want to continue the waiting while a condition is true, if it gets false I want to stop waiting. I'm trying right now catching the exception that appears in the await when the client closes, because the exception itself isn't that harmful to my program. – am.torrinha Apr 18 '19 at 14:48
  • But how does the condition change in your program? Is it in another task? Or something out of asyncio, or out of Python? How is it checked? – jdehesa Apr 18 '19 at 15:24
  • It was something I could verify with client.closed(), so while the client wasn't closed, I would need to keep waiting for data. But I solved it for now by catching the exception that the await raises. Thanks! – am.torrinha Apr 18 '19 at 15:33
  • @am.torrinha If you want to cancel `await` when the connection is closed, only way to do it is by catching the exception. – xyres Apr 18 '19 at 17:57

0 Answers0