_php Tutorial of pipeline communication Example of PHP multithreaded programming

Source: Internet
Author: User
Tags fread

A case analysis of pipeline communication in PHP multithreaded programming


This article mainly introduces the pipeline communication of the multi-threaded programming of PHP, the example analyzes the principle of the pipeline communication and related use skills, has a certain reference value, the need for friends can refer to the next

In this paper, we describe the usage of pipeline communication in PHP multithreaded programming. Share to everyone for your reference. The specific analysis is as follows:

A thread if it is a heroic heroism, then multi-threading is collectivism, you are no longer a loner, but a conductor.

Pipeline communication:
1. Pipelines can be thought of as a queue, and different threads can write things inside, and they can read from the inside. Write is
Added at the end of the queue, read is deleted in the team header.

2. Pipelines generally have size, the default is generally 4K, that is, the content of more than 4 K, you can only read, can not be written inside.

3. By default, after a pipeline is written, it is blocked until the read of his program reads the data. The read thread is also blocked,
Until a process writes data to the pipeline. Of course, you can change the default property by using the Stream_set_block function, which is set to non-blocking mode.

Here is a pipeline I split the class (this class naming problems, no unity, no time to change to unified, I generally first write the test code, the final sub-assembly, so the name may not be unified):

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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 The permission of the pipe, any user group can read and write by default

*/

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;

}

///////////////////////////////////////////////////

Write Pipeline function start

///////////////////////////////////////////////////

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

}

/////////////////////////////////////////////////////////

Read Pipeline correlation function start

////////////////////////////////////////////////////////

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 k\n";

$data. = Fread ($r _pipe, 1024);

}

Fclose ($r _pipe);

return $data;

}

function Close_read ()

{

Return fclose ($this->r_pipe);

}

/**

* Delete Pipeline

*

* @return Boolean is success

*/

function Rm_pipe ()

{

Return unlink ($this->fifopath);

}

}

?>

/*

With this class, you can implement a simple pipeline communication. */

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/963970.html www.bkjia.com true http://www.bkjia.com/PHPjc/963970.html techarticle A case analysis of pipeline communication of multi-threaded programming in PHP This article mainly introduces the pipeline communication of the multi-threaded programming of PHP, the example analyzes the principle of the pipeline communication and related use skills, has a certain reference ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.