Home > Backend Development > PHP Tutorial > PHP multi-threaded programming pipe communication example analysis_PHP tutorial

PHP multi-threaded programming pipe communication example analysis_PHP tutorial

WBOY
Release: 2016-07-13 10:05:21
Original
792 people have browsed it

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程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/963970.htmlTechArticlePHP多线程编程之管道通信实例分析 这篇文章主要介绍了PHP多线程编程之管道通信,实例分析了管道通信的原理与相关使用技巧,具有一定参考...
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