PHP multi-thread programming-Analysis of pipeline communication instances

Source: Internet
Author: User
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 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 ):

<? Phpclass Pipe {public $ javasopath; private $ w_pipe; private $ r_pipe; /*** automatically create an MPS queue ** @ param string $ name the MPs queue name * @ param int $ mode, by default, any user group can read/write */function _ construct ($ name = 'pipe', $ 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 ;} //////////////////////////////////////// /// // 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 {$ th Is-> 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-> export 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. */

I hope this article will help you with php programming.

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.