Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of NIO, BIO, AIO and PHP implementation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "NIO, BIO, AIO and PHP implementation example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "NIO, BIO, AIO and PHP implementation example analysis" this article.

What BIO, NIO, AIO?

BIO synchronously blocks IBO.

There is a partner to ask what is synchronization, what is blocking ah?

Synchronous / asynchronous blocking / non-blocking

Synchronization: two synchronization tasks depend on each other, and one task must be executed in some way that depends on the other. For example, in the A-> B event model, you need to complete A before you can execute B. In other words, the synchronous caller does not return until the callee has finished processing the request, and the caller waits for the result to return.

Async: two asynchronous tasks are completely independent, and one party's execution does not need to wait for the other's execution. In other words, an asynchronous call returns the result as soon as it is called, and does not need to wait for the result to return. When the result is returned, hold the result through a callback function or other means to do something about it.

Blocking: blocking is the initiation of a request, and the caller is waiting for the result of the request to return, that is, the current thread will be suspended, unable to perform other tasks, and can continue only when the conditions are ready.

Non-blocking: non-blocking is to initiate a request, and instead of waiting for the result to return, the caller can do something else first.

The above is the explanation of these four words, so put it on the computer IO, a more approachable explanation

BIO (Blocking I * O)

Well, let's take express delivery parts as an example. part of the work of an express delivery company is delivery parts, and its working mode is that it can only be delivered one by one. If you want to send express delivery, you have to queue up one by one. This is synchronization. Finally, it was your turn. You threw the express delivery to him, and he told you to wait. The courier staff said that we still have some information to enter behind us, and the express delivery needs to be checked. You can't leave until our express company has finished checking it. This is called blocking.

NIO (No-Blocking I * O)

Synchronous non-blocking Istroke O

Go on, take the courier company as an example. The express company found that some users lined up behind, waiting in line, and went to the express company next door after too long. What should I do? The express company thought of a way to buy a number dispenser and a batch of storage boxes. To a customer, put the express delivery in a storage box, and then give the user a number, and then another user, regardless of whether the previous delivery has been checked or not, or give him a storage box and send him a number. If there is no queue between different customers, it will be accepted as soon as it is accepted, which is non-blocking. Let's take a look at the interior. The express delivery still enters the information one by one, X-ray inspection, so that it runs synchronously, waiting for the courier to finish the inspection and call the number, and the customer can only leave the delivery point when the customer gets the receipt.

AIO (Asynchronous I * O)

Asynchronous non-blocking IO

Some Javaer called him NIO2, and the express company upgraded to make a delivery cabinet, and the customer sent it again. When he came, he put it in the delivery cabinet, and then he scanned the code to follow the dynamics of the cabinet, and the customer could leave. At this time, the service was accepted and could leave immediately. This is non-blocking. When the couriers come to collect the parts, they will pick up the items in the cabinet together, and the delivery points will deal with these couriers together. When they find that there is something wrong with them, they will not immediately stop their work and wait for the customer to come out, but will inform the customer to come and then continue to process the next delivery. This is asynchronism.

Asynchronous blocking IO

Synchronous / asynchronous blocking / non-blocking, these four nouns, two sets of sum, and one is asynchronous / blocking.

Well, let's first cite an example. At this delivery point, a group of customers came to send masks abroad. As there was a great possibility that they would fail the inspection, the express point left everyone behind. When all the shipments have been checked, they will be uniformly sent back to you. This is the blocking. When the courier checked the delivery, they found that the problem was not immediately notified to the customer to deal with, but put it aside and move on to the next one. This is asynchronism.

Pseudo-asynchronous IO

In this mode, the underlying implementation is multiple synchronous blocking BIO running at the same time.

Finally, let's sum up:

Blocking and non-blocking refers to whether the I / O operation immediately returns or blocks when it cannot read or write (write when the Nic is full / read when the Nic is empty); synchronous asynchronism refers to whether the read and write operation is synchronous or asynchronous when the data is already ready, with different stages.

Difference

Async / Sync in computer difference

The above are some examples to help you understand memory. Next, let's take a look at the computational implementation.

At first, the Web service provided by the computer adopts the CGI protocol, which is the pure BIO mode. A cgi process listens on a port and processes a request before it can receive the next http request. This is synchronization.

The actual experience of the customer is "asynchronous" because later optimized, the CGI program can respond to multiple http requests simultaneously by self-fork processes.

Note that the basis of our discussion here is asynchronous / synchronous on a single process.

Blocking / non-blocking in computer difference

Here we take the shopping process as an example. When you place an order, you need to do the following:

Are the goods available for sale?

Inventory quantity

User balance

Which discount rules are triggered

Lottery ticket validity

...

According to the general practice is to verify step by step, the last check is finished, and then carry on the next check, this is the way of blocking.

So how to do it in a non-blocking way? suppose that in a micro-service environment, goods, inventory, lottery tickets and promotions are all independent systems that invoke commodity services and initiate inspection requests for merchandise for sale; continue to call inventory services without waiting for commodity services to reply, initiate inventory requests for merchandise to be sold, and then send them out in turn. Check requests, so that 5 inspection requests are initiated at the same time, and finally, I wait for all their requests to reply to me, and then come together to verify that all the checks have been passed. The person who initiates the request without waiting for a response and then moves on to the next thing is called non-blocking.

What can PHP do?

PHP and BIO implementation

PHP has been implemented, this is the most basic. But usually when testing, it doesn't feel blocked. OK, let's do an experiment together to limit the process of nginx and php-fpm to one try. Php-fpm is a multi-process BIO, and now our strength has been changed to a single process.

Adjust Nginx configuration

Adjust / etc/nginx/nginx.conf file:

# # set the number of nginx worker to 1 worker_processes 1

All right, then let's check it out with the ps command.

Adjust PHP configuration

Adjust / etc/php/php-fpm/conf.d/www.conf file:

Pm = static pm.max_children = 1 pm.start_servers = 1 pm.min_spare_servers = 1 pm.max_spare_servers = 1

Find these configurations and change them to the values above.

The final results are as follows

I added sleep as soon as I added the first line to the index.php code.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report