Parse PHP to execute scripts in parallel in multiple processes

Source: Internet
Author: User
This article provides a detailed analysis of PHP multi-process parallel execution scripts. For more information, see this article because php processes do not support multithreading, in some scenarios, you can use php to implement multi-process to make up for this deficiency for convenience and performance improvement:

The code is as follows:

#! /Usr/bin/env php
$ Cmds = array (
Array ('/apps/bin/launcher. php', 'charge/promotion_props_stat.php', 'mobile', 1 ),
Array ('/apps/bin/launcher. php', 'charge/promotion_props_stat.php', 'mobile', 2 ),
Array ('/apps/bin/launcher. php', 'charge/promotion_props_stat.php', 'click', 1 ),
Array ('/apps/bin/launcher. php', 'charge/promotion_props_stat.php', 'click', 2 ),
Array ('/apps/bin/launcher. php', 'charge/promotion_props_stat.php', 1 ),
Array ('/apps/bin/launcher. php', 'charge/promotion_props_stat.php', 2)

Foreach ($ cmds as $ cmd ){
$ Pid = pcntl_fork ();
If ($ pid =-1) {// process creation failed
Die ('fork child process failure! ');
Else if ($ pid) {// processing logic of the parent process
Pcntl_wait ($ status, WNOHANG );
Else {// sub-process processing logic
Pcntl_exec ('/usr/local/bin/php', $ cmd );

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