Home >Backend Development >PHP Tutorial >Asynchronous Programming - How does php execute code asynchronously?
For example, if I want to record some log data, I don’t need to store it in real time. I want to execute the following code immediately after the code is executed asynchronously. I also don’t need callback processing. If I send the data to the message queue, it will also take time. I want to continue the following code in milliseconds. As for who it sends the data to, I don’t care how much time it takes to put it into the database. For example,
echo 1;
log(data);
echo 2;
I want the log function to be executed in the background. I don't care how much time it takes, but I don't have to wait for it. To me, it seems like it will be executed in milliseconds.
For example, if I want to record some log data, I don’t need to store it in real time. I want to execute the following code immediately after the code is executed asynchronously. I also don’t need callback processing. If I send the data to the message queue, it will also take time. I want to continue the following code in milliseconds. As for who it sends the data to, I don’t care how much time it takes to put it into the database. For example,
echo 1;
log(data);
echo 2;
I want the log function to be executed in the background. I don't care how much time it takes, but I don't have to wait for it. To me, it seems like it will be executed in milliseconds.
Send to message queue VS millisecond level >>> Where is the contradiction?
You can use Redis to create a queue. Redis operations read and write tens of thousands of times per second. Putting messages into Redis only requires one write operation, and the time consumption is less than milliseconds.
So I think using queues can completely meet the millisecond level requirements you mentioned.
In addition, no matter what means are used to perform asynchronous operations, there will always be time overhead, which is inevitable.
<code>//程序被阻塞10秒 shell_exec('timeout 10 vmstat 1 >/dev/null 2>&1 &'); //程序不会被阻塞 pclose(popen('timeout 10 vmstat 1 >/dev/null 2>&1 &', 'r')); //因此可以异步执行任务 pclose(popen("timeout 60 php /path/to/task.php '$arg' >/dev/null 2>&1 &", 'r'));</code>
The variable $arg is the parameter passed to the script task.php. This parameter is obtained through $argv[1] in task.php.
timeout 60 means the maximum execution time of the task.php script is 60 seconds. It can be removed if not needed. .
The essence of asynchronous implementation by pclose(popen()) is to open a process to execute blocking code.
It is suitable for asynchronous scenarios that do not require automatic return of results (callbacks) after execution is completed.
The string parameter $arg can be enclosed in single quotes, which can avoid the impact of some spaces, but it still has flaws.
It is best to serialize the string parameter to a file,
then pass the file path parameter to the script task.php ,
Let task.php read the file by itself, unserialize and deserialize the data.
The file name should be unique, for example, it can be the user ID or process PID plus a random time number:
<code>$filename = md5(uniqid($uid.'_', true)); $filename = md5(uniqid($getmypid().'_', true));</code>