PHP multi-thread programming-analysis of pipeline communication instances-PHP Tutorial

Source: Internet
Author: User
PHP multi-thread programming-analysis of pipeline communication instances. PHP multi-thread programming-pipeline communication example analysis this article mainly introduces PHP multi-thread programming-pipeline communication, the example analyzes the pipeline communication principle and related use skills, analysis of pipeline communication instances based on PHP multi-thread programming

This article mainly introduces PHP multi-thread programming for pipeline communication. The example analyzes the pipeline communication principles and related usage skills, and has some reference value. if you need it, you can refer to it.

This article describes the use of MPs in PHP multi-threaded programming. Share it with you for your reference. The specific analysis is as follows:

If a thread is personal heroism, multithreading is collectivism. you are no longer a lone hero, but a conductor.

Pipeline communication:
1. the pipeline can be considered as a queue. different threads can write or read from it. Write is
Add at the end of the queue, and read is deleted in the queue header.

2. the MPs queue size is generally 4 kB by default, that is, if the content exceeds 4 kB, you can only read and cannot write it into it.

3. by default, after the pipeline is written, it will be blocked until the program reads the data. The reading thread will also be blocked,
Until a process writes data to the MPs queue. Of course, you can change the default attribute and use the stream_set_block function to set it to the non-blocking mode.

The following is an MPs queue class that I installed separately (this class is named incorrectly and is not uniform, so there is no time to change it to uniform. I usually write the test code first and then load it separately, therefore, the names may not be uniform ):

?

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 $ export OPath;

Private $ w_pipe;

Private $ r_pipe;

/**

* Automatically create an MPs queue.

*

* @ Param string $ name the name of the MPs queue

* @ Param int $ mode: the permission of the media transcoding queue. by default, any user group can read and write data.

*/

Function _ construct ($ name = 'pipele', $ mode = 0666)

{

$ Export OPath = "/tmp/$ name.". posix_getpid ();

If (! File_exists ($ paiopath )){

If (! Posix_mkfifo ($ export OPath, $ mode )){

Error ("create new pipe ($ name) error .");

Return false;

}

} Else {

Error ("pipe ($ name) has exit .");

Return false;

}

$ This-> export OPath = $ export OPath;

}

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

// Pipeline function writing starts

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

Function open_write ()

{

$ This-> w_pipe = fopen ($ this-> export OPath, 'w ');

If ($ this-> w_pipe = NULL ){

Error ("open pipe {$ this-> javasopath} for write error .");

Return false;

}

Return true;

}

Function write ($ data)

{

Return fwrite ($ this-> w_pipe, $ data );

}

Function write_all ($ data)

{

$ W_pipe = fopen ($ this-> export OPath, 'w ');

Fwrite ($ w_pipe, $ data );

Fclose ($ w_pipe );

}

Function close_write ()

{

Return fclose ($ this-> w_pipe );

}

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

/// Start of the read pipeline function

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

Function open_read ()

{

$ This-> r_pipe = fopen ($ this-> export OPath, 'r ');

If ($ this-> r_pipe = NULL ){

Error ("open pipe {$ this-> OPath} for read error .");

Return false;

}

Return true;

}

Function read ($ bytes = 1024)

{

Return fread ($ this-> r_pipe, $ byte );

}

Function read_all ()

{

$ R_pipe = fopen ($ this-> export OPath, '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 );

}

/**

* Deleting an MPs queue

*

* @ Return boolean is success

*/

Function rm_pipe ()

{

Return unlink ($ this-> javasopath );

}

}

?>

/*

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

I hope this article will help you with php programming.

Pipeline this article mainly introduces the pipeline communication of PHP multi-thread programming. The example analyzes the pipeline communication principle and related usage skills, with some 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.