Is asynchrony that important in a single process?

醉折花枝作酒筹
Release: 2023-03-11 16:32:01
forward
1250 people have browsed it

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
Copy after login

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说明安装成功了
Copy after login

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路径 
        
Copy after login

Accept, receive.php

#!/usr/bin/php 
        
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!