tornado learning notes day04 execution order

Keywords: Front-end Python JSON Django Linux

Response output - > write

prototype

self.write() function

This is how it is defined in the source code

def write(self, chunk: Union[str, bytes, dict]) -> None:

Effect

Write chunk data to buffer

Four ways to refresh buffer

  • Program interrupt
  • Manual refresh
  • Buffer full
  • Encounter \n

When you write a print, it is not directly displayed on the black screen interrupt, but a walk to the buffer first

It's just that we can't demonstrate it in python. We have to demonstrate it in C language and Linux system. Neither of the two conditions is available

C program demonstration

Here we need g C C or g + +

# include<stdio.h>
int main(){
    while(1){
        printf("hello");
        sleep(0.05);
    }
    return 0;
}

Basics

Code demo

class WriteHandler(RequestHandler):

    def get(self):
        self.write("write page info tornado!")
        self.write("write page nicie tornado!")
        self.write("write page coll tornado!")
        self.write("write page beautiful tornado!")
        '''
        //You'll find that they're connected, because I'm in the buffer
        '''
        # Flushes the buffer and closes the current request channel
        self.finish()
        # If I don't write him, he will refresh when our program is finished
        # The following line is missing
        self.write("write page wonderful tornado!")

Using write method to write JSON data

Was there a JsonResponse in Django
For example, in Douban film review
Request JSON data when we scroll down
It is loaded locally by creating DOM

Interface call sequence

Method

initialize()

prepare()

  • Function: preprocessing method is called before executing the corresponding request method.
  • Be careful:
    • Any HTTP request will execute the prepare() method
    • This prepare is a bit like the middleware in Django, but the middleware can be executed before or after
    • This is only executed before the HTTP method
    • It can be used for some, such as anti crawler. If I don't want you to make a normal request and don't give you the response content, I'll just skip write directly with an error here
    • Judge whether the user meets the specification

HTTP method

Get (parameter after URL)

  • Advantages: fast
  • Disadvantages: low data load and low security

Post (parameters are packed separately)

  • Advantages: slow speed
  • Disadvantages: the amount of data carried is high and the security is relatively high
  • It is generally used to modify the data on the server. Use post and get for others

head

Similar to get request, but there is no specific content in the response, which is used to get the header. Generally, you will not use

delete

Requests the server to delete the

put

Deliver specified content from client to server

patch

Request to modify local content

options

Return URL supports all HTTP methods

Set default headers() method

Write error() method

On finish() method

  • Action: call after request processing.
  • Application:
    • We can clean up and release a resource in the method change
    • Or the processing of a log We usually don't deal with this memory release. We think this python also has its own garbage collection mechanism
    • We can analyze the original data
    • For example, for the identity statistics and preference judgment of visitors, it can be used as a reference for adjusting the content ranking of this site
  • Be careful:
    • Try not to output in this method
    • What we are doing here is some internal processing of the server, not the client

We can print it out and have a look

class IndexHandler(RequestHandler):
    def initialize(self) -> None:
        print("init_initialize")

    def prepare(self):
        print("prepare")

    def get(self):
        print("get_start")
        self.write("main page info tornado!")

    def set_default_headers(self) -> None:
        print(":set_default_headers")

    def write_error(self, status_code: int, **kwargs: Any) -> None:
        print("write_error")

    def on_finish(self) -> None:
        print("on_finish")

The order of the results is as follows

:set_default_headers
init_initialize
prepare
get_start
on_finish

Another way, with errors

class IndexHandler(RequestHandler):

    def initialize(self) -> None:
        print("init_initialize")

    def prepare(self):
        print("prepare")

    def get(self):
        self.send_error(500)
        print("get_start")
        self.write("main page info tornado!")

    def set_default_headers(self) -> None:
        print(":set_default_headers")

    def write_error(self, status_code: int, **kwargs: Any) -> None:
        print("write_error")
        self.write("Server internal error!!!")

    def on_finish(self) -> None:
        print("on_finish")

The order of the results is as follows

:set_default_headers
init_initialize
prepare
:set_default_headers
write_error
on_finish
get_start

Summary of execution sequence

  • Under normal circumstances, when no error is thrown
    • : set default headers: set headers
    • Init? Initialize: initialize processing
    • prepare: preprocessing, preprocessing also needs a header, so it's behind him
    • Get start: start processing
    • on_finish
  • When an error is thrown
    • set_default_headers:
    • init_initialize:
    • prepare:
    • Set default headers: execute the headers again, here
    • write_error:
    • on_finish:
    • get_start:

You need to remember this order. Actually, you don't need to. If you forget it, just come back to see it. But you need to understand the function of each function

Posted by chyan on Mon, 16 Mar 2020 20:31:09 -0700