PHP three a similar way to implement multithreading, PHP three kinds of multithreading similar
1. Curl_multi method
When multi-threading is required, you can use Curl_multi to request multiple operations at once, but Curl is the network communication, the efficiency and reliability is relatively poor.
function Main () {$sql = "Select waybill_id,order_id from Waybill where status>40 order by update_time DESC LIMIT 10 "; $data = Yii::app ()->db->createcommand ($sql)->queryall (); Yii Framework format foreach ($data as $k + $v) {if ($k% 2 = = 0) {//even send a URL $send _data[$k] [' url '] = '; $send _data[$k [' body '] = $v [' waybill_id ']; } else {//odd send another URL $send _data[$k] [' url '] = ' http://www.abc.com '; $send _data[$k [' Body ']=array ($v [' order_id '] = = Array (' extra ' = 16)); }} $back _data =sendmulitrequest ($send _data); Var_dump ($back _data); } function Sendmulitrequest ($send _data) {$params = array (); $curl = $text = Array (); $handle = Curl_multi_init (); foreach ($data as $k + $v) {if (Empty ($v [' url ')]) {$v [' url '] = ' http://www.xxx.com ';//if URL is emp Ty,set defalut URL} $reqBody = Json_encode ($v [' body ']); $reqStream = Array (' Body ' = $reqBody,); $encRequest = Base64_encode (Json_encode ($reqStream)); $params [' data '] = $encRequest; $curl [$k] = Curl_init (); curl_setopt ($curl [$k], Curlopt_url, $v [' URL ']); curl_setopt ($curl [$k], Curlopt_post, TRUE); curl_setopt ($curl [$k], Curlopt_header, 0); curl_setopt ($curl [$k], Curlopt_postfields, Http_build_query ($params)); curl_setopt ($curl [$k], Curlopt_returntransfer, 1); Curl_multi_add_handle ($handle, $curl [$k]); } $active = null; do {$MRC = Curl_multi_exec ($handle, $active); } while ($MRC = = Curlm_call_multi_perform); while ($active && $MRC = = CURLM_OK) {if (Curl_multi_select ($handle)! =-1) {do {$MRC = Curl_multi_exec ($handle, $active); } while ($MRC = = Curlm_call_multi_perform); }} foreach ($curl as $k = + $v) {if (Curl_error ($curl [$k]) = = "") {$text [$k] = (string) curl_ Multi_getcontent ($curl [$k]); } curl_multi_remove_handle ($handle, $curl [$k]); Curl_close ($curl [$k]); } curl_multi_close ($handle); return $text; }
2, through the Stream_socket_client method
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 '). ".
"; 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 }
The above is for you to share the three kinds of PHP implementation of multi-threaded similar method, I hope that everyone's learning has helped.
http://www.bkjia.com/PHPjc/1066501.html www.bkjia.com true http://www.bkjia.com/PHPjc/1066501.html techarticle PHP Three implementation of a similar method of multithreading, PHP three multithreading similar to 1, Curl_multi method when the need for multi-threading, you can use Curl_multi one-time request for multiple operations to complete, but C ...