Difference between @gen.coroutines and @tornado.web.asynchronous

436 views
Skip to first unread message

Sinan Postacı

unread,
Oct 21, 2015, 1:28:00 PM10/21/15
to Tornado Web Server
Hello,

I'm new to tornado and async coding. I understood how @gen.coroutines works but not sure about @asynchronous. Some examples use either one of them or both of them together.

I also read this;
"The @asynchronous decorator should be used to mark a method that is already asynchronous; it does not make the method asynchronous." (http://stackoverflow.com/questions/21907752/tornado-file-upload-is-blocking)
but I haven't understood why we should mark a method. 

i would appreciate if someone clear me up on this.

Thanks in advance.

A. Jesse Jiryu Davis

unread,
Oct 21, 2015, 2:12:11 PM10/21/15
to python-...@googlegroups.com
If a RequestHandler method like "get" or "post" uses callbacks, mark it with @asynchronous. This tells Tornado not to call RequestHandler.finish() when your method terminates — you promise to call finish() yourself. See the docs for details:

If this decorator is given, the response is not finished when the method returns. It is up to the request handler to call self.finish() to finish the HTTP request. Without this decorator, the request is automatically finished when the get() or post() method returns. Example:

class MyRequestHandler(RequestHandler):
    @asynchronous
    def get(self):
       http = httpclient.AsyncHTTPClient()
       http.fetch("http://friendfeed.com/", self._on_download)

    def _on_download(self, response):
       self.write("Downloaded!")
       self.finish()

If your method is a coroutine (it is decorated with @gen.coroutine and it executes "yield") then there is no need for @asynchronous.

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sinan Postacı

unread,
Oct 22, 2015, 1:04:55 AM10/22/15
to Tornado Web Server
I got it, thank you.
Reply all
Reply to author
Forward
0 new messages