Embodiment of the process of communication Swoole

Embodiment of the process of communication Swoole

pipelinepipe

Data exchange between the pipes for the process, Linux system itself provides a pipefunction to create a half-duplex communication pipe. Half-duplex communication in only one way flow of data (write-only end end of the read-only), only with the use of genetic relationship between process (parent and child) is.

Interprocess communication pipeline is IPCthe most basic way, there are two types of pipes are named pipes, anonymous pipes.

  • Anonymous pipes: designed for between processes with blood, and complete the data transfer.
  • Named pipes: can be used between any two processes, Swoole in the pipeline are anonymous pipe.

In the use of Swoole eventfdand UnixSockpackage two pipes, so that the communication between the process more flexible.

Swoole the Process module built pipelines way for inter-process communication, in the construction of the Process instance just open the $pipe_typeoption, Swoole underlying automatically creates a pipe, where the description of the need, although the name is called on the pipeline, but in fact in the new version Swoole the underlying communication is achieved through UnixSock, so it is not Linux Pipe in the true sense.

Create a process

swoole_process::__construct(
callable $function, 
bool $redirect_stdin_stdout = false, 
int $pipe_type = SOCK_DGRAM, 
bool $enable_coroutine = false
);

 

The type of pipe $pipe_typecan be divided into three types:

  • 0They said they did not create the pipe
  • 1It represents the creation of SOCK_STREAMtypes of pipes
  • 2It represents the creation of SOCK_DGRAMtypes of pipes

When enabled $redirect_stdin_stdout, the $pipe_typeoption ignores the user parameters, forced to 1.

Pipe descriptors

When the process is forkcame out, Process Object parent and child process will be provided on a named pipemember variable, the storage of the underlying UnixSocket descriptor, the parent and child processes may transmit data via the pipe descriptors may be directly call Process provides read/writean interface to send and receive data.

object(Swoole\Process)#1 (6) {
  ["pipe"]=>int(4)
  ["callback"]=>NULL
  ["msgQueueId"]=>NULL
  ["msgQueueKey"]=>NULL
  ["pid"]=>int(287)
  ["id"]=>NULL
}

 

If the process parameters specified when creating $pipe_typea non-zero value indicates use of the pipeline to create a way to be created after the success of the child process will generate a pipeline number pipe.

Each will create a process with the creation of a pipeline, the main process and the target process to communicate, you can write or read data to the target process piping.

Pipeline to read and write

  • swoole_process->write(string $data) Pipeline to the process of writing data
  • swoole_process->read(int $buffer_size = 8192) Reads data from the pipe in the process

Case: Close the standard input and output redirection child process

$ vim test.php

First save all child processes to handle the main process of an array, the array index to PID. When the main process and to correspond to a sub-process, the child process handles used to read and write data corresponding to the pipe, the pipe can be achieved inter-process communication.

<? PHP
 // process created after the success callback processing 
function handle (swoole_process $ worker ) {
     // read data from the process pipe 
    $ the Data = $ worker -> the Read ();
     echo  PHP_EOL "from Master: {. $ The Data } " ;
     // write data to process piping in 
    $ pipe = $ worker -> pipe; // pipes number of child processes 
    $ pid = $ worker -> pid; // PID of the child process 
    $ worker -> the write (" the Hello Master, the this pipe IS { $ pipe }, {PID IS $ PID } " );
     SLEEP(2 );
     $ worker -> Exit (0 ); 
} 

// number of processes 
$ worker_num = 2 ;
 // redirect the input and output 
$ redirect_stdin_stdout = to false ;
 // array storage process 
$ Workers = [];
 // loop to create multi-process 
for ( $ i = 0; $ i < $ worker_num ; $ i ++ ) {
     // create processes 
    $ process = new new swoole_process ( "handle", $ redirect_stdin_stdout );
     // start the process
    PID $ = $ Process -> Start ();
     // save the process handle 
    $ Workers [ $ PID ] = $ Process ; 
} 

// main process 
the foreach ( $ Workers  AS  $ PID => $ Process ) {
     // child process to handle own write data pipeline 
    $ process -> write ( "Hello worker, the this PID iS { $ PID }" );
     // child process to read data from its own handle pipe 
    $ data = $ process -> read () ;
     echo  value is PHP_EOL "worker from: {. $ Data }." value is PHP_EOL;
}                               

 

$ php test.php

from master: hello worker, this pid is 347
from worker: hello master, this pipe is 4, pid is 347

from master: hello worker, this pid is 348
from worker: hello master, this pipe is 6, pid is 348

 

Case: open standard input and output redirection child process

Setting creating process $redirect_stdin_stdoutis trueenabled, use in the process echowill not be printed to the screen, but will be written to the pipe, reads the keyboard input will change to read data from the pipe, the default is blocking read.

<? PHP
 // process created after the success callback processing 
function handle (swoole_process $ worker ) {
     // read data from the process pipe 
    $ the Data = $ worker -> the Read ();
     echo  PHP_EOL "from Master: {. $ The Data } " ;
     // write data to process piping in 
    $ pipe = $ worker -> pipe; // pipes number of child processes 
    $ pid = $ worker -> pid; // PID of the child process 
    $ worker -> the write (" the Hello Master, the this pipe IS { $ pipe }, {PID IS $ PID } " );
     SLEEP(2 );
     $ worker -> Exit (0 ); 
} 

// number of processes 
$ worker_num = 2 ;
 // redirect the input and output 
$ redirect_stdin_stdout = to true ;
 // store process array 
$ Workers = [];
 // loop to create multi-process 
for ( $ i = 0; $ i < $ worker_num ; $ i ++ ) {
     // create processes 
    $ process = new new swoole_process ( "handle", $ redirect_stdin_stdout );
     // start the process
    PID $ = $ Process -> Start ();
     // save the process handle 
    $ Workers [ $ PID ] = $ Process ; 
} 

// main process 
the foreach ( $ Workers  AS  $ PID => $ Process ) {
     // child process to handle own write data pipeline 
    $ process -> write ( "Hello worker, the this PID iS { $ PID }" );
     // child process to read data from its own handle pipe 
    $ data = $ process -> read () ;
     echo  value is PHP_EOL "worker from: {. $ Data }." value is PHP_EOL;
}                       

 

$ php test.php

from worker:
from master: hello worker, this pid is 350

from worker:
from master: hello worker, this pid is 351

 

message queuemessage queue

Linux is a series of message queue stored in the kernel of the message list, there is a message queue msgKey, you can access it through a different message queue. Message queue size limits data, the default is 8192, you can be modified by the kernel.

Swoole use message queue

  • Communication modes: default to scramble mode, unable to deliver the message to the child process specified.
  • After the new message queue, you can use the primary process.
  • Message queues and can not be used with the pipe, can not be used swoole event loop.
  • To call the primary process wait()function, otherwise the child process calls pop()or push()error.

Message Queuing function Swoole

  • swoole_process->useQueue()
  • swoole_process->push(string $data)
  • swoole_process->pop(int $max_size = 8192)

Case

<? PHP
 // process created after the success callback processing 
function handle (swoole_process $ worker ) {
     $ recv = $ worker -> POP ();
     echo  PHP_EOL "from Master: {. $ Recv }" ;
     SLEEP (2 );
     $ worker -> Exit (0 ); 
} 

// number of processes 
$ worker_num = 2 ;
 // redirect the input and output 
$ redirect_stdin_stdout = to false ;
 // store process array 
$ Workers = [];
 // loop to create multiple processes
for ( $ I = 0; $ I < $ worker_num ; $ I ++ ) {
     // create processes 
    $ Process = new new swoole_process ( "handle", $ redirect_stdin_stdout );
     // Message Queuing 
    $ Process -> useQueue ();
     // start process 
    $ PID = $ process -> start ();
     // save the process handle 
    $ Workers [ $ PID ] = $ process ; 
} 

// main process 
the foreach ( $ Workers  AS  $ PID=>$process){
    $process->push("hello worker, this pid is {$pid}");
}
              
for($i=0; $i<$worker_num; $i++){
    $ret = swoole_process::wait();
    $pid = $ret["pid"];
    unset($workers[$pid]);
    echo PHP_EOL."worker pid is {$pid} exit";
}

 

$ php test.php

from master: hello worker, this pid is 368
from master: hello worker, this pid is 369
worker pid is 368 exit
worker pid is 369 exit

 

Guess you like

Origin www.cnblogs.com/akidongzi/p/11608479.html