


PHP multi-threaded programming pipe communication example analysis_PHP tutorial
Analysis of pipeline communication examples in PHP multi-thread programming
This article mainly introduces pipeline communication in PHP multi-thread programming, and analyzes the principles and related usage techniques of pipeline communication with examples. It has certain reference value, friends in need can refer to it
The example in this article describes the usage of pipeline communication in PHP multi-threaded programming. Share it with everyone for your reference. The specific analysis is as follows:
If one thread is individual heroism, then multiple threads are collectivism. You are no longer a lone ranger, but a conductor.
Pipeline communication:
1. A pipeline can be thought of as a queue. Different threads can write things to it and read things from it. Just write
Adding at the end of the queue, reading means deleting at the head of the queue.
2. Pipes generally have a size, and the default is generally 4K. That is, if the content exceeds 4K, you can only read it but cannot write to it.
3. By default, after writing to the pipe, it will be blocked until the program that reads it has finished reading the data. And the reading thread will also be blocked,
Until a process writes data to the pipe. Of course, you can change this default attribute and use the stream_set_block function to set it to non-blocking mode.
The following is a pipeline class that I packaged (there is a problem with the naming of this class, it is not unified, and there is no time to change it to a unified one. I usually write the test code first and package it last, so the naming may not be unified):
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
class Pipe { public $fifoPath; private $w_pipe; private $r_pipe;
/** * Automatically create a pipeline * * @param string $name pipe name * @param int $mode Pipe permissions, by default any user group can read and 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; } /////////////////////////////////////////////////// // 写管道函数开始 /////////////////////////////////////////////////// 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); } ///////////////////////////////////////////////////////// /// 读管道相关函数开始 //////////////////////////////////////////////////////// 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 Kn"; $data .= fread($r_pipe, 1024); } fclose($r_pipe); return $data; } function close_read() { return fclose($this->r_pipe); } /** * Delete pipe * * @return boolean is success */ function rm_pipe() { return unlink($this->fifoPath); } } ?> /* 有了这个类,就可以实现简单的管道通信了。*/ |
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Java Made Simple: A Beginner's Guide to Programming Power Introduction Java is a powerful programming language used in everything from mobile applications to enterprise-level systems. For beginners, Java's syntax is simple and easy to understand, making it an ideal choice for learning programming. Basic Syntax Java uses a class-based object-oriented programming paradigm. Classes are templates that organize related data and behavior together. Here is a simple Java class example: publicclassPerson{privateStringname;privateintage;

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.
