- If code didn't use this decorator, is it non-blocking?
- Why this name is asynchronous, it means add decorator let code asynchronous?
- Why @tornado.gen always use with @tornado.web.asynchronous together?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Answered here: asynchronous vs non-blocking
Think of it like this. When you need to make a request to say a database or another url to retrieve data you do not want to block your tornado IO. So the
@tornado.web.asynchronous
will allow the IO to handle other requests while it waits for the content to load (ex. database or url).They are simular. You most likely will use
@tornado.web.asynchronous
.@tornado.web.asynchronous
is essentially a just a marker you put on a handler method likeget()
orpost()
that tells the framework that it shouldn't callfinish()
automatically when the method returns, because it contains code that is going to set upfinish()
to be called at a later time.@tornado.web.asynchronous
prevents the theRequestHandler
from automatically callingself.finish()
. That's it; it just means Tornado will keep the connection open until you manually callself.finish()
.Code not using this decorator can block, or not. Using the decorator doesn't change that in any way.
As @Steve Peak said, you use the decorator for asynchronous requests, e.g. database retrieval.
Updated for Tornado 3.1+: If you use
@gen.coroutine
, you don't need to use@asynchronous
as well. The older@gen.engine
interface still requires@asynchronous
, I believe.