PHP Multi-process

Source: Internet
Author: User
Tags message queue usleep

PHP Inter-process communication-Message Queuing

This article does not cover PHP base library installation. Detailed installation instructions, please refer to the official website, or look forward to follow-up blog share.

1. Message Queuing Function Preparation

<?php
Generate a key for Message Queuing
$msg _key = Ftok (__FILE__,' A ');
Generate a message queue
$msg _queue = Msg_get_queue ($msg _key,0666);
Detects whether a queue exists, returns a Boolean value
$status = Msg_queue_exists ($msg _key);
You can view some details about the current queue
$message _queue_status = Msg_stat_queue ($msg _queue);

Adding a message to a message queue
Msg_send ($msg _queue,1,"Hello, 1");
Msg_send ($msg _queue,1,' Hello, 2 ');
Msg_send ($msg _queue,1,"Hello, 3");

Reads a message from the message queue.
Msg_receive ($msg _queue,1,$message _type,1024,$message 1);
Msg_receive ($msg _queue,1,$message _type,1024,$message 2);
Msg_receive ($msg _queue,1,$message _type,1024,$message 3);

To remove a message queue
Msg_remove_queue ($msg _queue);
Echo$message 1. Php_eol;
Echo$message 2. Php_eol;
Echo$message 3. Php_eol;


/**
* Msg_send has three required parameters
* Resource $queue,
* int $msgtype,
* Mixed $message
*
* The first must be a queue resource type. Resource (4) of type (sysvmsg queue)
* The second parameter is the message type, one shaping, and must be greater than 0.
* Msg_send () sends a message of type Msgtype (which must be greater than 0) to the message queue specified by queue.
* A third parameter. Is the message to be sent. Can be a string, or it can be an array. It is serialize by default.
*/


/**
* Msg_receive parameters are much more. There are 5 parameters that must be filled in.
* Resource $queue,
* int $desiredmsgtype,
* Int & $msgtype,
* int $maxsize,
* Mixed & $message
*
* where $desiredmsgtype. After testing and the official website description does not match, temporarily does not explain.
*
* $msgtype. This is the msg_type selected in the Msg_send. This is a reference parameter.
* The type of the message that is received'll be is stored in this parameter.
*
* $maxsize.
* The maximum size of message to be accepted are specified by the maxsize;
* If the message in the ' queue is ' larger than this size the function would fail (unless you set flags as described below).
* This parameter declares a maximum message size, which will cause an error if it is exceeded.
*
* $message.
* The message type sent above msg_send.
*/

2. Multi-Process Communication instance

<?php
/**
* This code simulates a routine task.
* The first parent process produced a child process. The child process also acts as a parent process, producing 10 child processes.
* Can be simplified to a-B--c,d,e ... and other processes.
* As a, only production tasks are required and then assigned to B to handle. B will assign the task to 10 child processes for processing.
*
*/

Setup scripts never Time Out
Set_time_limit (0);
$ftok = Ftok (__FILE__,' A ');
$msg _queue = Msg_get_queue ($ftok);
$pidarr = [];

Generating child processes
$pid = Pcntl_fork ();
if ($pid) {
The parent process simulates generating an extra-large array.
$arr = Range (1,100000);

Put the task in the team and let multiple sub-processes work in parallel
foreach ($arrAs$val) {
$status = Msg_send ($msg _queue,1,$val);
Usleep1000);
}
$pidarr [] =$pid;
Msg_remove_queue ($msg _queue);
}else {
After the child process receives the task, fork10 the sub-process to process the task.
for ($i =0;$i <10;$i + +) {
$childpid = Pcntl_fork ();
if ($childpid) {
$pidarr [] =$childpid;Collecting child processes ProcessID
}else {
while (True) {
Msg_receive ($msg _queue,0,$msg _type,1024,$message);
if (!$message)Exit0);
Echo$message. Php_eol;
Usleep1000);
}
}
}
}

Prevents the main process from exiting before the child process, forming a zombie process
while (Count ( $pidarr) > 0) {
foreach ( $pidarr as $key = $pid) {
$status = Pcntl _waitpid ( $pid, $status);
if ( $status = =-1 | | $status > 0) {
unset ( $pidarr [ $key]);
}
}
Sleep (1);
}
?>

The above example is just to illustrate the application examples of multi-process communication and is not applied in real projects. For example convenience, a lot of validation conditions are omitted. But as an understanding of the process and principles, it does not affect.
You must use more than Usleep (1000) when executing a while loop. Otherwise the CPU may be blown up.
Above the multi-process communication, no zombie process is generated. Benefit from the last segment of the while loop.
The principle is that the parent process detects whether the child process exits at each iteration. If you exit, the parent process reclaims the child process. and removes the process from the list of processes.
You can use the PS aux |grep process.php to see how many processes are currently being generated. Where process.php is the name of the run
The effect is as follows:

[[Email protected]~]# PS aux |grep php
74:root41639.32.224390822844 pts/1 s+17:420:xx php process.php
75:root41640.00.32291043924 pts/1 s+17:420:xx php process.php
76:root41651.30.42291044124 pts/1 s+17:420:xx php process.php
77:root41661.30.42291044124 pts/1 s+17:420:xx php process.php
78:root41671.00.42291044124 pts/1 s+17:420:xx php process.php
79:root41681.30.42291044124 pts/1 s+17:420:xx php process.php
80:root41691.30.42291044124 pts/1 s+17:420:xx php process.php
81:root41701.30.42291044124 pts/1 s+17:420:xx php process.php
82:root41711.30.42291044124 pts/1 s+17:420:xx php process.php
83:root41721.30.42291044124 pts/1 s+17:42 0:00 php process.php
84:root 4173 1.3 0.4 229104 4124 pts/1 s+ 17:42 0:00 php process.php
85:root 4174 1.3 0.4 229104 4124 pts/1 s+ 17:42 0:00 php process.php

If you have any questions, you can discuss your study together. Bo Master is just learning this piece, if there is anything wrong, hope to get guidance, common improvement.

PHP Multi-process

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.