Flask source Code Tour (Basics) -WSGI

WSGI is what?

WSGI is an inextrable concept for learning Python Web programming, so the question is, what exactly is WSGI? And why do we need it? Before we begin our discussion of WSGI, let’s take a quick trip through history to the wartime era of Espionage 1938……

Small code is a heroic underground party member, his hand mastered a precious enemy information, urgent need to give this information to the organization.

Small code concealing found the leadership of the following dialogue:

Small size: “is it safe to lead you there?”

Leader: “I am safe! Is it safe on your side?”

Small size: “I’m safe!”

So the small size assured to hand over the information, the leadership gratified patted the small size of the shoulder: “the young man stem of good! Wait for notice.”

After that, the leader returned to the organization with the information and found a professional intelligence analyst in accordance with the constitution of the organization.

After an analysis, the intelligence personnel report back an analysis result to the leader in accordance with the organization’s rules.

The leaders get the results of the analysis, will inform the relevant situation to the small code, the heroic small code comrades will decide the next step according to this situation.

Now let’s illustrate the above story in a graphical way:

Guess what role WSGI plays in the story in Python Web?

.

Is leadership?

Ha ha ha, actually is the constitution of the organization! What about the code, the protocol, the leader, the analyst?

Here I provide another picture for you to compare:

Before and after the two pictures correspond, is it clear?

UWsgi is a Web server. The browser initiates an HTTP request. After receiving the request, the Web server invokes a Web application implemented by the Flask framework to process the request according to THE WSGI protocol. The Web server sends an HTTP reply to the browser.

The uWsgi web server role is also familiar to apache, and the same Web framework as Flask is well known in Python as Django.

The Web server is responsible for receiving the HTTP request, parsing the HTTP request, and invoking the subsequent handler based on the configuration. Flask is a framework that can be thought of as a set of tools that make it much easier to write and apply processors.

So what is Nginx? Every time we Google for flask deployments, nginx+uWsgi+ Flask fills the screen. Now let’s go back to the previous story……

Small code this kind of underground party member has a lot, distribute all over the country. Every day, information is like snowflakes pouring in from all directions. The ability of leaders is mainly reflected in their strong organizational ability, and they are competent to dispatch intelligence analysts to deal with information, but so many intelligence leaders simply cannot coordinate and deal with it.

What should you do at this point? People are not enough then add people! Therefore, the organization will add a number of offices, each office appointed leadership and intelligence analysis team, the problem can be solved naturally.

But, small code meng force! Of all the offices, which one should he be looking for? At this time, the organization thought of ways, one way is to set up another information reception. All information goes to the receiving office, which distributes it to the various offices according to some protocol, and the problem is solved again.

Getting back to the point, you probably already know that NGINx is the receiving end. Nginx has the advantage of high performance and can be specially used to receive requests. After Nginx, we can deploy multiple uWsgi servers and distribute requests to each uWsgi Server.

Here we go again. Yes, I am the problem baby. How does Nginx pass these requests to the various uWsgi Servers?

Above, we mentioned that the uWsgi server itself can receive intelligence directly, that is, it can handle HTTP requests directly. So will Nginx directly forward HTTP requests to uWsgi again?

It is possible in theory, but it is not done…… Because HTTP, as a text protocol, is simply not efficient and wastes space. So what to do? Why don’t we come up with a better, faster, stronger agreement?

So uwsgi came out of nowhere ~(PS: there is music ~ calabash baby ~ Calabash baby ~ Seven flowers on a vine ~ no fear of wax whipping ~)

That’s right. Spell it with me. It’s called U-W-S-G-I, not U-W-S-G-I. Uwsgi is a protocol. Uwsgi is a server program. Nginx can communicate with the UWSGI server through the UWSGI protocol.

So the new communication flow looks like this:

Well, this article is almost over now, yes, it is almost over, that is, it is not over yet ~ ha ha

Sharp-eyed children’s shoes must have been found (I know no one found……) “, I mentioned a question in the story above: “So many offices, which one should the small code find to hand in information? “One way is to set up a reception desk for information,” he said. So there are other ways

Just to give you a little bit more information, the way a browser accesses a Web service is usually through a domain name.

In other words, when handing in information, little code would rarely go directly to the meeting place, but would go to a place first and whisper, “Heaven cover earth tiger”, and someone would tell him to go to such and such place, where there is a man like wind waiting for you.

So the way to come, since you can tell small code this underground party members to go there to hand in information, so we in the wang handed in information when let him go to office A, in the xiaoli handed in information when tell him to go to office B can ah.

Then, an interactive diagram of the second solution is presented to all readers:

Finally, add the full interaction of the first option as well:

Well, this is really the end of the article! (Sigh of relief)

What? You ask me, what if Nginx performance in the first scenario is also insufficient? Please refer to the second scheme and combine it with the first scheme to see if you can resist it again. Haha.

Here is a cute TWO-DIMENSIONAL code ~ please try to sweep it, do not hesitate!