Question

I know coroutine in Python use get = yield ret , callee.send() , callee.next(). But I havn't find above things such as call.send() in Tornado Source code gen.py. How to explain coroutine in Tornado with an easy understand way? Without a Bigger picture, I just can't understand what Tornado did.

Was it helpful?

Solution

gen.py does call send(), but in Runner.run(), not in engine() or coroutine() as you might expect.

It seems that engine() and coroutine() basically evaluate the wrapped function to see whether it returns a generator. If it does, it calls Runner.run() on the result, which internally seems to loop over send(). It's not exactly obvious what it's doing though...

OTHER TIPS

The send and next functions are not features of Tornado, they are part of core Python. For more details see PEP 342 and this section of the official documentation.

You can write your own coroutines without tornado like so:

>>> def f():
...     x = 0
...     while True:
...         x = x*2
...         x = yield x
...
>>> coro = f()
>>> coro.next() #need to call 'next' once to start the coroutine
0
>>> coro.send(10)
20
>>> coro.send(100)
200
>>> coro.send(1000)
2000
>>>

The thing that tornado provides is an "event loop" which allows you to run a bunch of coroutines in a single process to achieve concurrency when there is blocking IO. For example in a web server that is reading data from a database and streaming the results to clients, each request will run in a separate coroutine, and tornado will handle the execution of all those coroutines concurrently by using yield/send/next at the points where IO occurs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top