Understanding @ gen.coroutine annotations

I know that my question looks broad, but I hope that the answer to this question will give me the right direction to read. I'm new to the Tornado framework, mostly I'm new to Python. I am reviewing this project : Could you explain me a few lines of code:

@gen.coroutine def get_me(self): raise gen.Return((yield self._api.get_me())) 
  • What does @gen.coroutine for annotation?
  • raise keyword is used for exceptions, right? Why are we using it here?
  • Why do we return everything in the form of a generator . Is the framework of the Tornado framework use generators. What is the reason?
+5
source share
2 answers
+3
source

Following the Tornado documentation, I found that a common way to provide asynchronous behavior is to use event loop and callback functions. But using callbacks is syntactically complicated and confusing. Therefore, tornado developers came up with the use of decorators (just like a bulb, cherry, etc.).

  • When you follow the Tornado source code, you will see the gen.py module under which they define the coroutine decorator. This is a truly elegant way to provide concurrency in a tornado.
  • raise designed to handle exceptions. I find this pretty simple, as it just returns except gen.Return .
  • Using Tornado generators easy to use.
+1
source

All Articles