When we are doing the project, some needs, especially the data response processing takes a lot of time, we all know that PHP itself does not support multi-threading, then how to implement the multi-threaded PHP?
1. PHP Multithreading under Linux
The following is the Pcntl_fork function that originates from PHP. Because this function relies on the implementation of the operating system fork, the things in this article only apply to Linux/unix. So let's take a look at the use of this function. This is what the PHP manual says:
<?php$pid = Pcntl_fork (), if ($pid = =-1) {die (' could not fork '),} else if ($pid) { ///We are the parent pc Ntl_wait ($status); Protect against Zombie Children} else { //We are the child}?>
Creating a subprocess through Pcntl_fork, if the return value is-1, indicates that the child process creation failed. The process ID that was created is returned to the parent process, and 0 is returned to the child process. It's hard to understand, so it should be written like this:
<?php$pid = Pcntl_fork (); if ($pid = =-1) { //create failed I'll quit, there's nothing to say Die (' could not fork ');} else{ if ($pid) { //The code written from here is the parent process, because the system program is written, remember to give a return value exit (0); } else{ ///From here to write the code is executed in the new process, the same normal exit, it is best to give a return value exit (0);} }? >
Such a change to understand more, if your parent process want to know that the child process gracefully exit, you can add the previous pcntl_wait.
2. By Stream_socket_client Way
function Sendstream () {$english _format_number = Number_format ($number, 4, '. ', '); echo $english _format_number; Exit (); $timeout = 10; $result = Array (); $sockets = Array (); $convenient _read_block = 8192; $host = "test.local.com"; $sql = "Select waybill_id,order_id from Xm_waybill where status>40 order by update_time desc LIMIT 1"; $data = Yii::app ()->db->createcommand ($sql)->queryall (); $id = 0; foreach ($data as $k = + $v) {if ($k% 2 = = 0) {$send _data[$k] [' body '] = noticeorder::getsenddata ($v [' WA ybill_id ']); } else {$send _data[$k] [' body '] = array ($v [' order_id '] = = Array (' extra ' + 16)); } $data = Json_encode ($send _data[$k] [' body ']); $s = stream_socket_client ($host. ":", $errno, $errstr, $timeout, Stream_client_async_connect | Stream_client_connect); if ($s) {$sockets [$id + +] = $s; $http _message = "Get/php/test.php?data=". $data . "Http/1.0\r\nhost:". $host. "\r\n\r\n"; Fwrite ($s, $http _message); } else {echo "Stream". $id. "Failed to open correctly."; }} while (count ($sockets)) {$read = $sockets; Stream_select ($read, $w = null, $e = null, $timeout); if (count ($read)) {/* Stream_select generally shuffles $read, so we need to compute from which socket (s ) we ' re reading. */foreach ($read as $r) {$id = Array_search ($r, $sockets); $data = Fread ($r, $convenient _read_block); if (strlen ($data) = = 0) {echo "Stream". $id. "Closes at". Date (' H:i:s '). ".<br>"; Fclose ($R); Unset ($sockets [$id]); } else {$result [$id] = $data; }}} else {/* A time-out means that *all* streams has failed to receive A response. */ echo "time-out!\n"; Break }} PRint_r ($result); }
3, through multi-process instead of multithreading
function Daemon ($func _name, $args, $number) {while (true) { $pid =pcntl_fork (); if ($pid ==-1) { echo "fork process fail"; Exit (); } ElseIf ($pid) {//created child process static $num =0; $num + +; if ($num >= $number) { //when the number of processes reaches a certain number, the child process is recycled. pcntl_wait ($status); $num--; } } else{//For 0 is a child process created, then directly into the working state if (function_exists ($func _name)) {while (true) { $ppid =posix_getpid ( ); Var_dump ($ppid); Call_user_func_array ($func _name, $args); Sleep (2); } } else{ echo "function is not exists"; } Exit (); } } } function Worker ($args) { //do something } daemon (' worker ', Array (1), 2);
Summarize:
PHP can not use multi-threaded, in fact, we all know, but we can get him through a lot of ways to achieve, this is the introduction of the multi-threaded implementation of PHP method. Hope to help you!
Related recommendations:
PHP Multi-Threading Implementation Example
PHP Multithreading One Implementation method-shell
PHP Multithreading Small case
about PHP multi-threading processing problem