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".

5
  • So you want to cancel the await if something in particular happens? You probably need to cancel the task somehow. Commented Apr 18, 2019 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. Commented Apr 18, 2019 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? Commented Apr 18, 2019 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! Commented Apr 18, 2019 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. Commented Apr 18, 2019 at 17:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.