tornado learning notes asynchrony in Day08 tornado

Summary

epoll is mainly used to solve the problem of network concurrency, so the asynchronous in tornado is mainly reflected in the IO asynchronous of the network, that is, asynchronous web request

tornado.httpclient.AsyncHTTPClient

tornado provides asynchronous web request clients, which can be used for asynchronous web requests, The client and the server are relatively speaking. When the Handler of tornado goes to other places to request resources, it is the client

fetch(request, callback=None)

Used to execute a web request and an asynchronous response returns a tornado.httpclient.httpresponse
request can be a url or a tornado.httpclient.httprequest object
If the url is inserted, a request object will be generated automatically

HTTPRequest

Summary

HTTP request class, whose constructor can receive parameters

parameter

  • url: string type, url to visit, required
  • Method: string type, HTTP request method
  • headers: dictionary type, or HTTPHeaders type
  • body: HTTP request body

HTTPResponse

Response class

attribute

  • Code: status code
  • reason: description of status code
  • body: data of response
  • error: exception

@tornado.web.asynchronous

Do not close the communication channel

Discard after ''tornado 6' '
@tornado.web.asynchronous
 Change to
@tornado.gen.coroutine

Example

Asynchronous implementation of callback function

Code example

class Students1Handler(RequestHandler):
    def on_response(self,response):
        print("Just entered on_response inside n")
        if response.error:
            self.send_error(500)
        else:
            print("Start getting data")
            data = json.loads(response.body)
            print("data Get success")
            self.write(data)
            print("data Write successfully")
        self.finish()

    @tornado.gen.coroutine
    def get(self):
        # Get information about all students
        # time.sleep(30)
        # Create client
        url = "http://127.0.0.1:8080/home"
        print("url yes",url)
        client = AsyncHTTPClient()
        print("Client created successfully")
        client.fetch(url, self.on_response)

        self.write("students info content!")

class HomeHandler(RequestHandler):
    def get(self):
        # Get information about all students
        self.write("homo page info!")

Implementation of asynchrony by cooperation

Code example 1

class Students2Handler(RequestHandler):
    @tornado.gen.coroutine
    def get(self, *args, **kwargs):
        url = "http://s.budejie.com/topic/tag-topic/64/hot/budejie-android-6.6.9/0-20.json?market=xiaomi&ver=6.6.9&visiting=&os=7.1.1&appname=baisibudejie&client=android&udid=863254032906009&mac=02%3A00%3A00%3A00%3A00%3A00"
        client = AsyncHTTPClient()
        res = yield client.fetch(url)
        if res.error:
            self.send_error(500)
        else:
            data = json.loads(res.body)
            self.write(data)

Code example 2, separate asynchronous web requests

class Students3Handler(RequestHandler):
    @tornado.gen.coroutine
    def get(self, *args, **kwargs):
        res = yield self.getData()
        self.write(res)

    @tornado.gen.coroutine
    def getData(self):
        url = "http://s.budejie.com/topic/tag-topic/64/hot/budejie-android-6.6.9/0-20.json?market=xiaomi&ver=6.6.9&visiting=&os=7.1.1&appname=baisibudejie&client=android&udid=863254032906009&mac=02%3A00%3A00%3A00%3A00%3A00"
        client = AsyncHTTPClient()
        res = yield client.fetch(url)
        if res.error:
            ret = {"ret":0}
        else:
            ret = json.loads(res.body)
        raise tornado.gen.Return(ret)
class HomeHandler(RequestHandler):
    def get(self):
        # Get information about all students
        self.write("homo page info!")

Tags: Front-end JSON Android network Mac

Posted on Tue, 17 Mar 2020 11:10:42 -0400 by aynsoft