Example of method to implement order delay processing in PHP
Recently, when doing business, I need to implement the function of automatic cancellation after the customer places an order after the order times out and fails to pay. I have just confirmed several methods: The client requests cancellation at the time and checks whether the server has a scheduled time. Orders that need to be canceled are then processed in batches. Create a timer after placing the order. Use redis or memcache for delayed processing. Set the expiration time and delete automatically.
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 always be in an unprocessed state. ; The second method is more commonly used, but it has accuracy issues and the cycle of scheduled tasks needs to be confirmed, 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 and cannot be counted. Unprocessed data (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 PHP used, if you want to implement the timer function, you need to use Swoole or workerman. Since Swoole is an extension framework developed by C, its performance is definitely better, so I chose Swoole.
Preparation
To use Swoole, you first need to install the
Swooleextension 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
phpinfoorPHP-m, ifSwooleappears , it means the installation is successfulSwooleThe official document has timer related documents
Start testing
We create a swoole_test.php file and a log.txt file (for testing), swoole_test.phpThe 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 on the Linux terminal: /usr/local/php7/bin/php /home/app/swoole_test.php , the results are as follows:

I felt a burst of heart. . .
原来定时器只能在 cli 模式下,那么这个想法怕是要GG了,难道就栽倒这里了吗,难道就没有别的方法了吗?就在我欲哭无泪的时候突然灵光乍现,一个词闪到我的脑海: Python !
对,我们不能单单靠着 PHP 啊,还有 Python 这种神奇的语言呢,我们知道 Python 的 os 模块里的 os.system 方法是可以执行命令行的,那么不就可以实现在 cli 模式下运行刚才的 swoole_test.php 文件了么。
内心一阵激动后,觉得测试是否可行
我们知道 Linux 都是自带 Python 的,但是不同的版本 Python 版本不同,有的自带的是 Python2.6 ,版本过低了,所以需要装一个高版本的,这里我选择 Python3 ,注意不要覆盖系统自带的 Python2 。以下是大致的安装步骤:
wget http://python.org/ftp/python/... 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
接下来终端输入: Python3 ,如果出现

则安装成功。
安装完 Python3 之后,我们新建一个 test.py 文件,内容如下:
#!usr/bin/env python3`
#-*- coding:utf-8 -*-
import os
ret = os.system("/usr/local/php7/bin/php /home/app/swoole_test.php")
#请使用自己系统的绝对路径
print(ret)
然后我们在终端执行: /usr/bin/python3 /home/app/test.py ,注意:这里只是执行 PHP 文件,但是文件里的 echo 内容是不会在终端输出的,这时候就用到刚才新建的 log.txt 文件了。执行完 Python 文件后,我们去log文件检查下,发现内容已经写入,所以使用 Python 是可以实现 PHP 的 cli 模式的。┗|`O′|┛ 嗷~~

到这里就会有同学疑惑了,你这使用 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的吧。
The above method can actually completely omit the
Pythonstep. The reason why I did not remove it is to write down my implementation experience, because I think I may really encounter it during the development. Seeing this superfluous approach, in short, we need to think more, read more code, and find solutions that can be optimized. I feel that I am far behind here, so please share your encouragement
Related recommendations:
Example detailed explanation of the tab switching effect of Vue imitating Taobao order status
PHP implements the RSA signature generation order function using Alipay as an example
The above is the detailed content of Example of method to implement order delay processing in PHP. For more information, please follow other related articles on the PHP Chinese website!
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AMPHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AMIn PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.
PHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AMPHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AMKey players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AMIn PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.
PHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AMPHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.
PHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AMPHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.
Why Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AMThe core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools







