Nginx: Asynchronous non-blocking IO

Source: Internet
Author: User

In the socket programming, often see blocking, non-blocking, synchronous, asynchronous, then what is the relationship between them and what is the difference?

This time the Nginx asynchronous non-blocking event-driven model to explain the relationship between them.

Blocking IO

In Linux, all sockets are blocked by default.

This means that when you use the socket to invoke a function such as recv, the function will not return until no data arrives, causing the thread to block until the data arrives.

Non-blocking IO

We can use FCNTL to set the socket to non-blocking.

This means that when a function such as recv is called using the socket, the function returns immediately and can determine if there is a data arrival based on the return value.

People who believe that blocking/non-blocking IO are very clear, this time is mainly about the difference between synchronous IO and asynchronous IO.

The first thing to say is that synchronous/asynchronous is only related to specific implementations, meaning that non-blocking must be asynchronous, and that there is no definite relationship between them.

Synchronous IO

In general, the functions we call are synchronous.

What is synchronization? That means we call a function, and after that function returns, it means that what the function is doing is done.

This is our general practice, for example, that we call recv to receive data, and after that function returns, it means that we have received the good data.

Asynchronous IO

Asynchronous IO can probably understand this:

We call a function that, after the function returns, does not actually complete the function, but is managed elsewhere (such as Nginx event-driven).

It is visible that the function is not done immediately (but will eventually be done by the managed module).

Perhaps the reader will have this question: How is asynchronous IO implemented? What application does it have? The following will take the Nginx event-driven model to solve these problems.

Application of asynchronous non-blocking IO

Take an example that is more easily understood:

When processing an HTTP request, if you want to ignore the contents of the request package (we must receive the package, but do not do the processing), we will ngx_discard_reqeust_body the function.

Ignoring the request package body may be a long process, but we don't need to ignore the contents of the package (which we don't care about), so the function is asynchronous and non-blocking:

Thus, after calling the function we can proceed to execute the following code, and the action to receive and ignore the package body is managed to the event driver to complete.

Implementation of asynchronous non-blocking IO in Nginx

Nginx's high concurrency comes from its asynchronous, non-blocking event driver, which is illustrated by the Nginx Epoll-based event-driven model.

The use of Epoll and the Nginx event-driven model can be viewed in the first two parts of http://www.cnblogs.com/runnyu/p/4914698.html, which will not be described in detail here.

It can be seen that the Nginx main loop calls the Ngx_epoll_process_events method, which calls epoll_wait to get the event and handle the event.

We just call Epoll_ctl to add or delete events (managed events) to Epoll, and the Nginx event driver will help us handle the event when the event is ready.

Nginx: Asynchronous non-blocking IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.