A detailed explanation of the delay processing of the PHP implementation order

Source: Internet
Author: User
Tags php cli
This article mainly introduces the use of PHP how to achieve order delay processing of relevant information, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends below with the small series to learn together.

Business requirements

Orders are a feature that we often encounter in our daily development, and we have recently started to confirm several methods when we need to implement the automatic cancellation of the order timeout after the customer orders the business.

    • Client-to-time request cancellation

    • Server-side timing query There is no need to cancel the order, and then batch processing

    • Create timer after order, delay processing

    • Use Redis or memcache storage, set expiration time, automatically delete

Comprehensive consideration of the above method, the first one to exclude, because if the customer put the app background prohibit or network connection is forbidden, then cannot send to the server request, the order will always be unhandled state, the second method uses more, but there is the problem of accuracy, there is a need to confirm the timing of the task cycle, Temporarily listed as a post-repair method; The problem with the fourth method is that if the deletion is a physical deletion, the data cannot be statistically processed (you can, of course, have a database such as MySQL for long-term storage and then use the method two timing processing) when you save Redis.

Finally prepare to use method three.

When using Method 3, it is necessary to use Swoole or Workerman to implement the timer function because of the development language of PHP. Because Swoole is a C development of the extension framework, performance is certainly better than the choice of swoole.

Pre-preparation

    • Using Swoole first needs to install the Swoole extension on the server, the installation method and the installation of other extensions are similar, you can refer to this article

    • After installation, check whether the extension is installed properly, view phpinfo or php-m, if Swoole is present, the installation is successful.

    • Swoole Official document with timer related documents

Start testing

We create a swoole_test.php file and a Log.txt file (for testing), and the swoole_test.php code is as follows:

<?phpswoole_timer_after ((), 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 on the Web page to access this PHP file, the results are as follows:

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

A burst of heart ...

The original timer only in the CLI mode, then this idea is afraid of GG, is it fell here, there is no other way? Just as I was about to cry without tears, a word flashed into my mind: python!

Yes, we can't just rely on PHP, and the magic of Python language, we know that the Python OS module Os.system method can execute the command line, then can not implement in the CLI mode to run just the swoole_test.php file.

After a burst of excitement, I think the test is feasible

We know that Linux is a python, but different versions of Python version, some of which comes with Python2.6, the version is too low, so need to install a high version, here I choose Python3, pay attention not to cover the system comes with the Python2. 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 terminal input: Python3, if it appears


The installation succeeds.

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

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

Then we do in the terminal:/usr/bin/python3/home/app/test.py, note: Here just executes the PHP file, but the file echo content is not in the terminal output, this time to use the newly created Log.txt file. After executing the python file, we go to log file check and find that the content has been written, so using Python can implement PHP CLI mode. ┗| ' O′|┛ AW ~


There will be a classmate puzzled, you use Python to implement the PHP CLI mode, but how to remote access through the Web? This is the time to use the PHP exec method, we know that the Exec method of PHP and Python Os.system method is able to execute command line commands, so we can create a new test.php file, the content is as follows:

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

Then we access the test.php file via the Web page. The results are as follows:


Then go to log file check, found also write log, so this method is feasible!

Do here in the heart flattered, but the old feel like what is wrong, finally finally realize a very silly problem: since PHP can directly have command line function, why superfluous with Python and then in Python function? It's not the pants, the fart superfluous?

Again big scold oneself is the silly force n times, I silently modified the test.php file content:

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

With direct access to the test.php file, the feedback results are like python, which eliminates the python step and executes PHP files directly with the EXEC function of PHP.

End

After the test passed, it was found that this method can be used to create a timer and use the Web remotely, but there is a problem If I use the same as the above with the Web simulation will find that the page refresh is to wait until the end of test.php execution, that is, if we set the timer time to 30 minutes will have to wait 30 minutes to have feedback information, this method will not work, so need to use asynchronous access, such as the use of Web-based Ajax technology and other asynchronous technology, I won't repeat it here.

Tail

These are just my thoughts and implementation steps to solve the problem, to the real development may not choose this way, because there is no performance testing, and for process Control and threading control does not have much in-depth understanding, so in the future to do the order automatically canceled or will choose Method 2 Bar.
In fact, the above method can completely eliminate the python step, I did not remove the reason is to write my implementation experience, because I think the development period may really encounter this superfluous way, in short, to think more, look at the code, find the solution to optimize, here feel very far away, mutual encouragement Bar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.