PHP multi-process programming (2) pipeline communication

Source: Internet
Author: User
PHP multi-process programming (2) pipeline communication if a process is a personal heroism, then multiple processes are collectivism. (Do not strictly distinguish the differences between multi-process and multi-thread)

You are no longer a lone ranger, but a conductor.

It is very free to be unique. However, in many cases, it is not as high as gathering firewood.

This is my understanding of multi-process. The main problem of multithreading programming is communication and synchronization.

For more background knowledge about PHP multi-threaded programming, see:

PHP multi-process programming (1)

In PHP, if pcntl is used, it is very difficult to implement simple communication.

The following describes MPs queue 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 read process 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 ):

Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/-->
 Export OPath = $ export OPath ;} //////////////////////////////////////// /// // start the function of writing the MPs queue /////////////////////// /// // function open_write () {$ this-> w_pipe = fopen ($ this-> export OPath, 'w'); if ($ this-> w_pipe = NULL) {error ("open pipe {$ this-> export OPath} 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 the function related to the read pipeline /////////////// //////////////////////////////////////// /function open_read () {$ this-> r_pipe = fopen ($ this-> export OPath, 'r'); if ($ this-> r_pipe = NULL) {error ("open pipe {$ this-> fif OPath} for read error. "); return false;} return true;} function read ($ byte = 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 );} //////////////////////////////////////// //// // *** delete the MPs queue ** @ return boolean is success */function rm_pipe () {return unlink ($ this-> export OPath) ;}}?>

With this class, you can implement simple pipeline communication, because this tutorial is part of the multi-process programming series.

The application part of this pipeline class will be placed in the third part.

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.