It is extremely difficult to read and understand code split into different methods, such as the asynchronous code that requires working with callbacks that are executed once the asynchronous execution finishes. Luckily, Tornado provides a generator-based interface that enables us to write asynchronous code in request handlers in a single generator. We can avoid splitting our methods into multiple methods with callbacks by using the tornado.gen
generator-based interface that Tornado provides to make it easier to work in an asynchronous environment.
The recommended way to write asynchronous code in Tornado is to use coroutines. Thus, we will refactor our existing code to use the @tornado.gen.coroutine
decorator for asynchronous generators in the required methods that process the different HTTP requests in the subclasses of tornado.web.RequestHandler
.