Home > Article > Backend Development > Detailed explanation of delayed processing of orders in PHP
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 great detail through sample code. It has certain reference and learning value for everyone's study or work. Friends who need it can follow Let’s learn together with the editor.
Business Requirements
Orders are a function we often encounter in daily development. Recently, when doing business, we need to implement automatic cancellation after the customer places an order and the order times out and is not paid. function, we have just confirmed several methods:
The client requests cancellation at the time
The server regularly queries whether there is anything that needs to be canceled Order, and then batch processing
Create a timer after placing the order, delayed processing
Use redis or memcache storage, set the expiration time, automatically Delete
Considering the above methods, the first one is eliminated first, because if the customer disables the APP background or network connection, then the request cannot be sent to the server, and the order will remain. It is in an unprocessed state; the second method is used more often, but there are accuracy issues and the need to confirm the period of the scheduled task, so it is temporarily listed as a backup method; the problem with the fourth method is that if the order is deleted, it will be physically Deleted, unprocessed data cannot be counted (of course, you can store it in a database like mysql for long-term storage when saving 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.
Preparation
To use Swoole, you first need to install the Swoole extension on the server. The installation method is similar to installing other extensions. You can refer to this article
After installation, check whether the extension is installed normally. Check phpinfo or PHP-m. If Swoole appears, it means the installation is successful.
Swoole official documentation is available Timer related documents
Start testing
We create a swoole_test.php file and a log.txt file (for testing), swoole_test.php The code is as follows:
<?php swoole_timer_after(3000, function () { append_log(time()); echo "after 3000ms.\n"; }); function append_log($str) { $dir = 'log.txt'; $fh = fopen($dir, "a"); fwrite($fh, $str."\n"); fclose($fh); }
Then access this PHP file on the web page, the result is as follows:
Then run PHP in the Linux terminal: /usr/local/php7/bin/php /home/ app/swoole_test.php, the results are as follows:
I felt a burst of 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
#!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~~
<?php $program="/usr/bin/python3 /home/app/nongyephp/test.py"; #注意使用绝对路径 echo "begin<br>"; (exec ($program)); echo "end<br>"; die;Then we access the test.php file through the web page. The results are as follows:
然后去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那一步,我没有去掉的原因是把我的实现经历写出来,因为我觉得开发期间可能真的会遇到这种多此一举的方式,总之是要多思考,多看代码,找出能优化的方案,这里感觉自己差得很远,共勉吧
相关推荐:
The above is the detailed content of Detailed explanation of delayed processing of orders in PHP. For more information, please follow other related articles on the PHP Chinese website!