March 2019
Beginner
490 pages
12h 40m
English
Another way to write asynchronous code in Tornado is by using coroutines. Instead of using a callback function for processing the response, we can use the yield keyword to resume and suspend the execution. Tornado 2.1 introduced the tornado.gen.coroutine module, which provides a pattern for performing asynchronous requests.
You can find the following code in the tornado_request_async_coroutine.py file:
#!/usr/bin/python3import tornado.ioloopimport tornado.webimport tornado.httpclientclass Handler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.coroutine def get(self): http_client = tornado.httpclient.AsyncHTTPClient() response = yield tornado.gen.Task(http_client.fetch, "https://www.google.com/search?q=python") ...
Read now
Unlock full access