yar安装使用

1.安装

pecl install yar

vim /etc/php.ini 加上extension=yar.so

查看支持的配置:
php --re yar

- Dependencies {
    Dependency [ json (Required) ]
  }
  - INI {
    Entry [ yar.packager ]      //打包协议
      Current = 'php'
    }
    Entry [ yar.transport ]
      Current = 'curl'
    }
    Entry [ yar.debug ]
      Current = 'Off'
    }
    Entry [ yar.expose_info ]
      Current = 'On'
    }
    Entry [ yar.connect_timeout ]
      Current = '1000'
    }
    Entry [ yar.timeout ]
      Current = '5000'
    }
    Entry [ yar.content_type ]
      Current = 'application/octet-stream'
    }
    Entry [ yar.allow_persistent ]
      Current = '0'
    }
  }

2.代码

   服务端,放到网站根目录/var/www/html:

//模拟实际情况
    $arg = func_get_args();
echo "test3 method with arg 0[$arg[0]] \n";
    return "ret3:".date("y-m-d H:i:s");
}
public function test4(){
    sleep(4);             //模拟实际情况
    $arg = func_get_args();
    echo "test4 method with arg 0[$arg[0]] \n";
    return "ret4:".date("y-m-d H:i:s");
}
protected function client_can_not_see() {
    echo __FILE__;
}
protected function client_can_not_see2() {
    echo __FILE__;
}
}
$service = new Yar_Server(new API());
$service->handle();
通过浏览器访问,如下:
yar安装使用_第1张图片
客户端:

test1("single arg1");
$c->test2("single arg2");
$c->test3("single arg3");
$c->test4("single arg4");
echo str_repeat("-",30),"time:",time()-$start,str_repeat("-",30),"\n";

echo str_repeat("-",40),"concurrent",str_repeat("-",40),"\n";
$start = time();
 Yar_Concurrent_Client::call($url,"test1",array("arg1"),"callback");
 Yar_Concurrent_Client::call($url,"test2",array("arg1"),"callback");
 Yar_Concurrent_Client::call($url,"test3",array("arg1"),"callback");
 Yar_Concurrent_Client::call($url,"test4",array("arg1"),"callback");
 Yar_concurrent_Client::loop();
 function callback($retval,$info){
   $args = func_get_args();
   $args['time'] = date("y-m-d H:i:s");
   var_dump($args);
 }
echo str_repeat("-",30),"time:",time()-$start,str_repeat("-",30),"\n";


3.测试

php yar_client.php

----------------------------------------single request----------------------------------------
test1 method with arg 0[single arg1] 
test2 method with arg 0[single arg2] 
test3 method with arg 0[single arg3] 
test4 method with arg 0[single arg4] 
------------------------------total time:10s------------------------------
----------------------------------------concurrent----------------------------------------
array(3) {
  [0]=>
  string(22) "ret1:14-06-12 19:35:03"             //进入时间
  [1]=>
  array(3) {
    ["sequence"]=>
    int(1)
    ["uri"]=>
    string(31) "http://localhost/yar_server.php"
    ["method"]=>
    string(5) "test1"
  }
  ["time"]=>
  string(17) "14-06-12 19:35:03" 
}
array(3) {
  [0]=>
  string(22) "ret2:14-06-12 19:35:04"        //进入时间
  [1]=>
  array(3) {
    ["sequence"]=>
    int(2)
    ["uri"]=>
    string(31) "http://localhost/yar_server.php"
    ["method"]=>
    string(5) "test2"
  }
  ["time"]=>
  string(17) "14-06-12 19:35:04"
}
array(3) {
  [0]=>
  string(22) "ret3:14-06-12 19:35:05"            //进入时间
  [1]=>
  array(3) {
    ["sequence"]=>
    int(3)
    ["uri"]=>
    string(31) "http://localhost/yar_server.php"
    ["method"]=>
    string(5) "test3"
  }
  ["time"]=>
  string(17) "14-06-12 19:35:05"
}
array(3) {
  [0]=>
  string(22) "ret4:14-06-12 19:35:06"                     //进入时间
  [1]=>
  array(3) {
    ["sequence"]=>
    int(4)
    ["uri"]=>
    string(31) "http://localhost/yar_server.php"
    ["method"]=>
    string(5) "test4"
  }
  ["time"]=>
  string(17) "14-06-12 19:35:06"
}
------------------------------total time:4s------------------------------


可见,四个函数在串行时,用时10s,在并发时,仅4s.提高了效率。

你可能感兴趣的:(php,高级)