Multi-threaded PHP programming-analysis of pipeline communication instances and multi-threaded programming instances. PHP multi-threaded programming-pipeline communication instance analysis, multi-threaded programming example analysis this article describes the use of PHP multi-threaded programming-pipeline communication. Share it with you for your reference. Analysis of multi-thread programming in PHP: Analysis of pipeline communication instances and analysis of multi-thread programming instances
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 ("ope N pipe {$ this-> 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. */
I hope this article will help you with php programming.
Pipeline this article describes the use of pipe communication in PHP multi-threaded programming. Share it with you for your reference. Specific analysis...