亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

php語言

php三種實(shí)現(xiàn)多線程類似的詳解

時(shí)間:2024-08-07 17:06:30 php語言 我要投稿
  • 相關(guān)推薦

php三種實(shí)現(xiàn)多線程類似的詳解

  php三種實(shí)現(xiàn)多線程類似的方法,需要的朋友可以參考下,就跟隨百分網(wǎng)小編一起去了解下吧,想了解更多相關(guān)信息請(qǐng)持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  1、curl_multi方法

  當(dāng)需要多線程的時(shí)候,可以用curl_multi一次性請(qǐng)求多個(gè)操作來完成,但curl走的是網(wǎng)絡(luò)通信,效率與可靠性就比較差了的。

  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 框架格式

  foreach ($data as $k => $v) {

  if ($k % 2 == 0) { //偶數(shù)發(fā)一個(gè)網(wǎng)址

  $send_data[$k]['url'] = '';

  $send_data[$k]['body'] = $v['waybill_id'];

  } else { //奇數(shù)發(fā)送另外一個(gè)網(wǎng)址

  $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 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、通過stream_socket_client 方式

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  34

  35

  36

  37

  38

  39

  40

  41

  42

  43

  44

  45

  46

  47

  48

  49

  50

  51

  52

  53

  54

  55

  56

  57

  58

  59

  60

  61

  62

  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 . ":80", $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 have failed

  to receive a response. */

  echo "Time-out!\n";

  break;

  }

  }

  print_r($result);

  }

  3、通過多進(jìn)程代替多線程

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  34

  35

  36

  37

  function daemon($func_name,$args,$number){

  while(true){

  $pid=pcntl_fork();

  if($pid==-1){

  echo "fork process fail";

  exit();

  }elseif($pid){//創(chuàng)建的子進(jìn)程

  static $num=0;

  $num++;

  if($num>=$number){

  //當(dāng)進(jìn)程數(shù)量達(dá)到一定數(shù)量時(shí)候,就對(duì)子進(jìn)程進(jìn)行回收。

  pcntl_wait($status);

  $num--;

  }

  }else{ //為0 則代表是子進(jìn)程創(chuàng)建的,則直接進(jìn)入工作狀態(tài)

  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);

  以上就是為大家分享的三種php實(shí)現(xiàn)多線程類似的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

【php三種實(shí)現(xiàn)多線程類似的詳解】相關(guān)文章:

PHP實(shí)現(xiàn)多線程的方法03-29

如何解決PHP無法實(shí)現(xiàn)多線程的問題03-02

php實(shí)現(xiàn)圖片縮放功能類03-31

基于PHP+Ajax實(shí)現(xiàn)表單驗(yàn)證的詳解03-03

PHP中讀取大文件實(shí)現(xiàn)方法詳解11-30

PHP對(duì)象Object的概念詳解11-16

PHP源代碼方式詳解11-17

php摘要生成函數(shù)詳解03-02

PHP中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)12-06