Home is well aware that PHP does not support multithreading. But it's really a headache for someone who needs to be like a multithreaded feature. Fortunately, there are several scenarios that can be solved, such as multithreading capabilities. Here is a summary of the three ways to implement multithreading similar methods, the following are three scenarios and code examples.
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";//i F URL isEmpty,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]) = = "") {$te xt[$k] = (string) curl_multi_getcontent ($curl [$k]); } curl_multi_remove_handle ($handle, $curl [$k]); Curl_close ($curl [$k]); } curl_multi_close ($handle); return $text; } 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 ur L is empty,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 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 [' waybill_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); } 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% = = 0) {$send _data[$k] [' body '] = noticeorder::getsenddata ($v [' waybill_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} daemon (' Worker ', Array (1), 2); 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);
http://www.bkjia.com/PHPjc/477201.html www.bkjia.com true http://www.bkjia.com/PHPjc/477201.html techarticle home is well aware that PHP does not support multithreading. But it's really a headache for someone who needs to be like a multithreaded feature. Fortunately there are several solutions that can be solved, like multithreading ...