search
HomePHP FrameworkSwooleHow to communicate between swoole processes

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
);

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}

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!