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.