_php Techniques for analyzing communication instances between PHP processes

Source: Internet
Author: User

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.

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.