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 Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version