A case analysis of pipeline communication in PHP multithreaded programming _php Skills

Source: Internet
Author: User
Tags fread php class php programming

This article illustrates the usage of pipeline communication in PHP multithreaded programming. Share to everyone for your reference. The specific analysis is as follows:

If a thread is a personal heroism, so many threads is collectivism, you are no longer a lone Ranger, but a conductor.

Pipeline communication:
1. Pipelines can be thought of as a queue, and different threads can write things inside, and they can read from inside. Write is
Added at the end of the queue, read is deleted at Team head.

2. The pipeline generally has the size, the default generally is 4K, is the content more than 4 K, you can only read, cannot write inside.

3. By default, after a pipe is written, it is blocked until read by reading his program. And the read thread is blocked,
Until a process writes data to the pipe. Of course, you can change this default property, using the Stream_set_block function, set to a non blocking mode.

The following is a pipe I separate the class (this class naming problem, there is no unity, no time to be unified, I generally write test code, the last part of the installation, so the name may not be unified):

<?php class Pipe {public $fifoPath;
  Private $w _pipe;
 
  Private $r _pipe; /** * Automatically create a pipe * * @param string $name pipe name * @param the permissions of the int $mode pipe, the default any user group can read/write/function __construct (
    $name = ' pipe ', $mode = 0666) {$fifoPath = "/tmp/$name.". Posix_getpid (); if (!file_exists ($fifoPath)) {if (!posix_mkfifo ($fifoPath, $mode)) {error ("Create new pipe ($name) error."
        );
      return false;
      } else {error ("pipe ($name) has exit.");
    return false;
  } $this->fifopath = $fifoPath; /////////////////////////////////////////////////////write pipe function start//////////////////////////////////////////////////
    /function Open_write () {$this->w_pipe = fopen ($this->fifopath, ' W ');
      if ($this->w_pipe = = NULL) {error ("Open pipe {$this->fifopath} for write error.");
    return false;
  return true;
  function Write ($data) {return fwrite ($this->w_pipe, $data); } functiOn Write_all ($data) {$w _pipe = fopen ($this->fifopath, ' W ');
    Fwrite ($w _pipe, $data);
  Fclose ($w _pipe);
  function Close_write () {return fclose ($this->w_pipe); ////////////////////////////////////////////////////////////Read pipe correlation function started/////////////////////////////////////////
    function Open_read () {$this->r_pipe = fopen ($this->fifopath, ' R ');
      if ($this->r_pipe = = NULL) {error ("Open pipe {$this->fifopath} for read error.");
    return false;
  return true;
  function Read ($byte = 1024) {return fread ($this->r_pipe, $byte);
    function Read_all () {$r _pipe = fopen ($this->fifopath, ' R ');
    $data = ';
      while (!feof ($r _pipe)) {//echo "read one k\n";
    $data. = Fread ($r _pipe, 1024);
    } fclose ($r _pipe);
  return $data;
  function Close_read () {return fclose ($this->r_pipe); /** * Delete Pipeline * * @return The Boolean is success/functIon Rm_pipe () {return unlink ($this->fifopath); }?>/* With this class, you can implement simple pipe communication. */

I hope this article will help you with your PHP programming.

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.