PHP multi-thread programming --- pipe communication-PHP source code

Source: Internet
Author: User
PHP multi-thread programming --- pipe communication

/* Pipeline communication: 1. pipeline can be considered as a queue. different threads can write or read from it. Write is added 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 is also blocked until a process writes data to the pipeline. 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 name may not be uniform): This code comes from: http://www.cnblogs.com/niniwzw/archive/2010/01/20/1652801.html */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-> 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-threaded 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.