Home  >  Article  >  Backend Development  >  Is asynchrony that important in a single process?

Is asynchrony that important in a single process?

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-07-16 16:17:161325browse

Generally speaking, the program in PHP is a single process. After one sentence is executed, the next sentence will be executed. But if it gets stuck in a certain link, then there is no way to execute the program. We need another mechanism to solve this problem, which is asynchronous.

Is asynchrony that important in a single process?

Under normal circumstances, the program in PHP is a single process. After executing one sentence, it will continue to the next sentence (the pcntl_fork() series of functions of PHP are not used here. Mine is This method is much better than that), just like a group of well-educated and hungry people (if you don’t eat this pair of rice, you may die, everyone wants to eat this meal as quickly as possible) queuing up to buy food Similarly, you must finish buying one before buying the next one. If a person takes all 1 jiao change, he must count all the 1 jiao and 1 jiao coins before he can buy the next one.

It may not be a problem to be late for a moment or two in the actual queue to buy food, but in a system that needs to respond quickly to users, it will be troublesome if this phenomenon occurs, just like a complete login operation. There are many steps. If they are executed step by step, if they are stuck in one step, they will be finished. The user will see that the loading button keeps turning... At this time, a mechanism is needed to solve this problem.

First of all, let’s learn about PHP’s inter-process communication extension, sysvmsg. It should be noted here that this extension can only be used in linux/uinux and is invalid on other platforms. My environment is centos6.3. It is very difficult to install this extension. Simple:

yum -y install php-process

Of course, the premise is that you have installed php. After executing it, use the following command to check whether the installation is successful:

php -m | grep sysvmsg
#若果看到sysvmsg说明安装成功了

#或者也可以这样
php -r 'var_dump(function_exists("msg_get_queue"))';
#若果看到true说明安装成功了

Of course, the above command needs to add the php path. to the system environment variables.

This extension can communicate between processes. Let’s look at an example.

Send, send.php

#!/usr/bin/php
#上面的是我自己的php路径
<?php
$ip = msg_get_queue(12340);
//创建一个队列
msg_send($ip,1,"Test a message",false,false,$err);//像队列中塞一条消息

Accept, receive.php

#!/usr/bin/php
<?php
$ip = msg_get_queue(12340);
//创建消息队列,和发送的要一致,不然收不到消息
while(msg_receive($ip,0,$msgtype,512,$data,false,null,$err)){
    echo "使用内存: ".memory_get_usage()."\n";
    //这里看下使用了多少内存
    echo "收到的消息: $data\n";
    //收到的消息在这里
}

It can be found by executing the program that sending messages and receiving messages follow the standard FIFO of the queue, so that we can These characteristics allow you to design an asynchronous system.

How to use these features, think about it this way, there will definitely be many operations when the program is executed. Some operations must be performed all the time, while some operations can be delayed, and some operations are inherently It's not important and it will take a lot of time.

For example, record the login log and record the game details. At this time, we don’t have to wait for the program to complete (if we wait, there may be problems. When there are too many unimportant operations, a lot of waste will be done. time), but directly throw the things to be operated into the queue.

Then open a separate process in the background to execute the received message, just like in receive.php, it keeps waiting there. When there is a message, it will run, and when there is no message, it will block. This is Doesn't it successfully solve the problem of wasting time by not executing the program immediately when the program is running?

Recommended learning: php video tutorial

The above is the detailed content of Is asynchrony that important in a single process?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete