This article describes the method of communication between PHP processes. Share to everyone for your reference, specific as follows:
PHP Single Process single-threaded processing batch task too slow, not bird, but PHP can not multithreading, and finally chose the process of multitasking batch task.
The PHP multi process uses for split primarily, then uses the Unix/linux semaphore to carry on the interprocess communication.
The example used is: producer => Consumer => collector, the model .
<?php//===== global variable =====//IPC interprocess communication $key = Ftok (__file__, "a");
$queue = Msg_get_queue ($key);
Process ID $producer _pid = 0;
$consumers _pid = Array ();
$collector _pid = Posix_getpid ();
===== Consumer ===== for ($i =0; $i < 2; $i + +) {$consumer _pid = Pcntl_fork ();
if ($consumer _pid = = 1) {exit ("could not fork!\n");
else if ($consumer _pid) {//pcntl_wait ($status);
echo "Consumer_pid: $consumer _pid\n";
$consumers _pid[] = $consumer _pid;
else {$pid = Posix_getpid ();
echo "Consumer_pid: $pid start\n";
while (true) {msg_receive ($queue, $pid, $msgtype, 1024, $message);
if ($message = = "Exit") {break;
}//Data processing $n = Intval ($message);
Msg_send ($queue, $collector _pid, $n * $n);
} exit ("Consumer ok!\n");
}//===== creator ===== $producer _pid = Pcntl_fork ();
if ($producer _pid = = 1) {exit ("could not fork!\n");} else if ($producer _pid) {//pcntl_wait ($status); echo "Producer_pid: $producer _piD\n ";
else {$pid = Posix_getpid ();
echo "Producer_pid: $pid start\n";
$n = 0;
For ($i =0 $i < $i + +) {foreach ($consumers _pid as $consumer _pid) {$n + +;
Msg_send ($queue, $consumer _pid, $n);
Sleep (1);
foreach ($consumers _pid as $consumer _pid) {msg_send ($queue, $consumer _pid, "exit");
Sleep (1);
Msg_send ($queue, $collector _pid, "exit");
Exit ("producer ok!\n");
}//===== collector ===== while (true) {msg_receive ($queue, $collector _pid, $msgtype, 1024, $message);
if ($message = = "Exit") {break;
echo sprintf ("% 5d:%d\n", $msgtype, $message);
} exit ("Collector ok!\n");
For more information on PHP related content readers can view the site topics: "PHP process and Threading Skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP date and Time usage summary", "PHP object-oriented Program Design Primer", "PHP Array ( Array), "Summary of PHP string usage", "Getting Started with Php+mysql database operations" and "Summary of common PHP database Operations Tips"
I hope this article will help you with the PHP program design.