Table of Contents
Preparation
Start testing
结尾
尾巴
Home Backend Development PHP Tutorial Example of method to implement order delay processing in PHP

Example of method to implement order delay processing in PHP

Mar 09, 2018 pm 05:50 PM
php deal with method

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 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 The 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 = &#39;log.txt&#39;;
    $fh = fopen($dir, "a");
    fwrite($fh, $str."\n");
    fclose($fh);
}
Copy after login

Then access this PHP file on the web page, the result is as follows: Example of method to implement order delay processing in PHP

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

Example of method to implement order delay processing in PHP

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

接下来终端输入: Python3 ,如果出现

Example of method to implement order delay processing in PHP

则安装成功。

安装完 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)
Copy after login

然后我们在终端执行: /usr/bin/python3 /home/app/test.py ,注意:这里只是执行 PHP 文件,但是文件里的 echo 内容是不会在终端输出的,这时候就用到刚才新建的 log.txt 文件了。执行完 Python 文件后,我们去log文件检查下,发现内容已经写入,所以使用 Python 是可以实现 PHPcli 模式的。┗|`O′|┛ 嗷~~

Example of method to implement order delay processing in PHP

到这里就会有同学疑惑了,你这使用 Python 实现了 PHPcli 模式,但是怎么通过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;
Copy after login

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

Example of method to implement order delay processing in 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;
Copy after login

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

结尾

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

尾巴

  • 以上只是我想到解决问题的想法和实施步骤,到了真正开发可能不会选择这种方式,因为没有经过性能测试,而且对于进程控制和线程控制并没有多深入的了解,所以以后做订单自动取消还是会选择方法2的吧。

  • The above method can actually completely omit the Python step. 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:

Detailed explanation of PHP using redis queue to realize automatic confirmation of receipt of e-commerce orders

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles