Home > PHP Framework > Swoole > body text

How to communicate between swoole processes

(*-*)浩
Release: 2019-12-02 14:19:13
Original
2968 people have browsed it

How to communicate between swoole processes

Swoole inter-process communication method

Pipe pipe

Pipe is used between processes For data interaction, the Linux system itself provides the pipe function for creating a half-duplex communication pipeline. In the half-duplex communication method, data can only flow in one direction (one end only reads and the other end only writes), and can only be used between processes that have a relationship (parent-child process). (Recommended study: swoole video tutorial)

Pipes are the most basic way of inter-process communication IPC. There are two types of pipes: named pipes and anonymous pipes.

Anonymous pipe: specially used to complete data transfer between processes with blood relationship. Named pipes: Can be used between any two processes. The pipes in Swoole are anonymous pipes.

Use eventfd and UnixSock to encapsulate two kinds of pipes in Swoole, making the communication between processes more flexible.

Swoole's Process module has a built-in pipeline for inter-process communication. When building a Process instance, as long as the $pipe_type option is turned on, the bottom layer of Swoole will automatically create a pipeline. If there is any need to explain here, Although it is called a pipe in name, the underlying communication in the new version of Swoole is actually implemented through UnixSock, so it is not a Linux Pipe in the true sense.

Create process

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

Pipe type $pipe_type can be divided into three types:

0 means not to create a pipe

1 means creating a SOCK_STREAM type pipe

2 means creating a SOCK_DGRAM type pipe

When $redirect_stdin_stdout is enabled, the $pipe_type option will ignore the user parameters and be forced to 1.

Pipe descriptor

When the process is forked, the Process objects in the parent process and the child process will be set with a member variable named pipe, which stores The descriptor of the underlying UnixSocket. The parent process and the child process can send data through this pipe descriptor, or they can directly call the read/write interface provided by Process to send and receive data.

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

Pipeline reading and writing

swoole_process->write(string $data) Write data to the process's pipe

swoole_process-> read(int $buffer_size = 8192) reads data from the process's pipe

The above is the detailed content of How to communicate between swoole processes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!