In Tornado, we usually write the following code to call a function asynchronously:
class MainHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def post(self):
...
yield self.handleRequest(foo)
...
@tornado.gen.coroutine
def handleRequest(self, foo):
...
But in asyncio (will be shipped with Python 3.4, can be installed from pip for Python 3.3), we use yield from
to achieve the same thing:
@asyncio.coroutine
def myPostHandler():
...
yield from handleRequest(foo)
...
@asyncio.coroutine
def handleRequest(foo)
...
Seeing from the code, the difference is yield
and yield from
. However the former handleRequest(foo)
returns a tornado.concurrent.Future
object, the latter returns a generator
object.
My question is, what is the difference between the two things in mechanism? How is the control flow? And who calls the actual handleRequest
and retrieves its returning value?
Append: I have basic knowledge of Python generators and iterators. I wanted to understand what Tornado and asyncio achieved by using these, and what is the difference between those two mechanisms.