Home >Backend Development >PHP Tutorial >Detailed explanation of how PHP implements delayed processing of orders

Detailed explanation of how PHP implements delayed processing of orders

韦小宝
韦小宝Original
2017-12-30 16:12:221387browse

This article mainly introduces to you the relevant information on how to use PHP to implement delayed processing of orders. The article introduces it in detail through the PHP sample code, which is very useful for everyone. Learning PHP or working has certain reference learning value. Friends who need it can follow the editor to learn together.

Business requirements

Order is a function that we often encounter in daily development. Recently, when doing business It is necessary to realize the function of automatic cancellation after the customer places an order after the order times out and fails to pay. We have just confirmed several methods:

  • The client requests cancellation at the time

  • The server periodically checks whether there are any orders that need to be canceled, and then processes them in batches

  • Create a timer after placing the order, and delay the processing

  • Use redis or memcache storage, set expiration time, and automatically delete

Considering the above methods, the first one is eliminated first, because if the customer bans the APP background or network connection, then If the request cannot be sent to the server, the order will always be in an unprocessed state; the second method is more commonly used, but there are accuracy issues, and the cycle of the scheduled task needs to be confirmed, so it is temporarily listed as a supplementary method; The problem with the four methods is that if the order is deleted, it will be physically deleted, and unprocessed data cannot be counted (of course, you can store it in a database like mysql for long-term storage when storing redis, and then use method 2 for regular processing).

Finally prepare to use method three.

When confirming the use of method 3, due to the development language of PHP, you need to use Swoole or Workerman to implement the timer function. Since Swoole is an extension framework developed in C, it is definitely better in terms of performance, so I chose Swoole.

Preliminary preparation

  • To use Swoole, you first need to install the Swoole extension on the server, installation method and installation Other extensions are similar, you can refer to this article

  • After installation, check whether the extension is installed normally, check phpinfo or PHP-m, if Swoole appears, the installation is successful

  • Swoole official documents have timer related documents

Start testing

We create a swoole_test.php file and a log.txt file (for testing). The swoole_test.php code is as follows:

<?php
swoole_timer_after(3000, function () {
 append_log(time());
 echo "after 3000ms.\n";
});
function append_log($str) {
 $dir = &#39;log.txt&#39;;
 $fh = fopen($dir, "a");
 fwrite($fh, $str."\n");
 fclose($fh);
}

Then access it on the web page For this PHP file, the results are as follows:

Then run PHP in the Linux terminal: /usr/local/php7/bin/php /home/app/swoole_test.php, the results are as follows:

There was a feeling in my heart. . .

It turns out that the timer can only be used in cli mode, so this idea is probably going to be GG. Is this where it falls? Is there no other way? Just when I was about to cry without tears, suddenly an idea suddenly occurred to me, and a word flashed into my mind: Python!

Yes, we can't just rely on PHP, there is also a magical language like Python. We know that the os.system method in Python's os module can execute the command line, so no Can you run the swoole_test.php file just now in cli mode?

After a burst of excitement, I felt that the test was feasible

We know that Linux all comes with Python, but different versions have different Python versions, and some come with it. It is Python2.6. The version is too low, so you need to install a higher version. Here I choose Python3. Be careful not to overwrite the Python2 that comes with the system. The following are the approximate installation steps:

  • wget http://python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz

  • tar xf Python-3.6.0.tar.xz

  • cd Python-3.6.0

  • ./ configure --prefix=/usr/local/python3

  • ##make && make install

  • ln -s /usr/local/python3/bin /python3 /usr/bin/python3

Next enter the terminal: Python3, if


appears, the installation is successful .


After installing Python3, we create a new test.py file with the following content:

#!usr/bin/env python3`
#-*- coding:utf-8 -*-
import os
ret = os.system("/usr/local/php7/bin/php /home/app/swoole_test.php") #请使用自己系统的绝对路径
print(ret)

Then we execute it in the terminal: /usr/bin/python3 /home/app/test.py, note: this is just executing the PHP file, but the echo content in the file will not be output on the terminal. At this time, the newly created log.txt file will be used. . After executing the Python file, we checked the log file and found that the content had been written, so PHP's cli mode can be implemented using Python. ┗|`O′|┛ Wow~~


到这里就会有同学疑惑了,你这使用Python实现了PHP的cli模式,但是怎么通过web远程访问呢?这个时候就用到PHP的exec方法了,我们知道PHP的exec方法和Python的os.system方法一样是可以执行命令行命令的,所以我们可以新建一个test.php文件,内容如下:

<?php
$program="/usr/bin/python3 /home/app/nongyephp/test.py"; #注意使用绝对路径
echo "begin<br>";
(exec ($program));
echo "end<br>";
die;

然后我们通过网页访问test.php文件。结果如下:


然后去log文件检查,发现也写入日志了,所以这个方法是可行的!

做到这里心里美滋滋的,不过老觉得好像哪里不对,终于终于意识到一个很傻逼的问题:既然PHP可以直接有命令行函数,为啥多此一举借助Python然后在用Python的函数呢?这不是脱了裤子放屁多此一举吗?

再大骂自己是傻逼N遍之后,我默默修改了test.php文件内容:

<?php
echo "begin<br>";
$program="/usr/local/php7/bin/php /home/app/nongyephp/swoole_test.php"; #注意使用绝对路径
(exec ($program));
echo "end<br>";
die;

在直接访问test.php文件,反馈结果和借助Python一样,这样就可以免去Python那一步,直接用PHP的exec函数来执行PHP文件。

结尾

测试通过后发现这种方法是可以创建定时器并且通过web远程使用的,不过有个问题,如果用和我上述一样用网页模拟会发现网页刷新是要等test.php执行完才会结束,也就是说如果我们把延时器的时间设成30分钟会要等待30分钟才会有反馈信息,这种方式肯定行不通的,所以需要使用异步访问,比如使用web的ajax技术和其他异步技术,这里不再赘述

尾巴

以上只是我想到解决问题的想法和实施步骤,到了真正开发可能不会选择这种方式,因为没有经过性能测试,而且对于进程控制和线程控制并没有多深入的了解,所以以后做订单自动取消还是会选择方法2的吧。
上述方法其实完全可以省掉Python那一步,我没有去掉的原因是把我的实现经历写出来,因为我觉得开发期间可能真的会遇到这种多此一举的方式,总之是要多思考,多看代码,找出能优化的方案,这里感觉自己差得很远,共勉吧

以上就是本篇文章的所有内容,希望对大家有所帮助!

相关推荐:

消息队列 - 有没有好的php延时队列

php函数的引用 php延时函数 php排序函

PHP关键字标红处理类

The above is the detailed content of Detailed explanation of how PHP implements delayed processing of orders. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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