I began looking into Tornado and started reading the user guide. However I find the stuff about coroutines very confusing. On one hand the documentation tells you how the decorator @gen.coroutine
makes things easier, but on the other hand it tells you, that:
Python 3.5 introduces the async and await keywords (functions using these keywords are also called “native coroutines”). Starting in Tornado 4.3, you can use them in place of yield-based coroutines. Simply use async def foo() in place of a function definition with the @gen.coroutine decorator, and await in place of yield. The rest of this document still uses the yield style for compatibility with older versions of Python, but async and await will run faster when they are available: [...]
So now I am thinking: "OK well, simply use async
and await
all the time and be done with it." However, in all the examples, the documentation then goes on always using the decorator instead of the keyword async
.
There is a section about how to call a coroutine. Is what the documentation writes there still true for the native keywords?
What are these decorators giving me, so that I should use them instead of native Python 3.5 syntax?
(I am using Python 3.5 for this learning project of mine, so I don't care about backward compatibility.)
Like the documentation says, if you're in Python 3.5 use async and await, not the "gen.coroutine" decorator and "yield". In Python 3.5, there is no advantage to using the decorator and "yield".
The tutorial uses the older style for the sake of compatibility with older versions of Python.
To call a coroutine in Python 3.5, use "await" instead of "yield". Everything else in that section of the guide is still correct.