肥宅自学平台_人人成为技术开发者

肥宅人只等待您,联系我们吧,曾经的巅峰是否能回来?我们正在用心服务!

实现纯异步PHP程序:php_http_parser

 更新时间:2024-05-07 14:10:29   作者:肥宅-季波   我要评论(0)  

         ibcurl提供了异步调用方式,有两种风格:

ONE MULTI HANDLE MANY EASY HANDLES:加入多个easy handle后执行curl_multi_perform方法。该方法在php curl扩展中有对应实现。但最后一步curl_multi_perform是阻塞的。

MULTI_SOCKET,这个是真正的非阻塞方法,但需要自行实现event loop,且封装较为困难,目前在php中没有对应实现。经过调研,curl_multi_socket_action跟php内核结合困难度很高。

除此之外,基本上没有真正的实现异步http请求的php扩展。目前仅有部分纯php实现的版本,比如tsf中的http client实现。 使用纯php实现的问题主要受限于http解析的性能。因此考虑将这一模块用扩展的方式来实现。node.js http-parser就是一个很好的c语言的http解析库。 php_http_parser就是对其做的一个封装,在php中暴露出相应的接口。

为了实现真正的非阻塞请求,仍然需要自己实现event loop。目前推荐结合swoole使用,以获得更好的性能。

使用方式
  1. $buffs = array("HTTP/1.1 301 Moved Permanently\r\n"
  2. ,"Location: http://www.google.com/\r\n"
  3. ,"Content-Type: text/html; charset=UTF-8\r\n"
  4. ,"Date: Sun, 26 Apr 2009 11:11:49 GMT\r\n"
  5. ,"Expires: Tue, 26 May 2009 11:11:49 GMT\r\n"
  6. ,"Cache-Control: public, max-age=2592000\r\n"
  7. ,"Server: gws\r\n"
  8. ,"Content-Length: 193\r\n"
  9. ,"\r\n"
  10. ,"<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n"
  11. ,"<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
  12. ,"<H1>301 Moved</H1>\n"
  13. ,"The document has moved\n"
  14. ,"<A HREF="http://www.google.com/">here</A>.\r\n"
  15. ,"<A HREF="http://www.google.com/">here</A>.\r\n"
  16. ,"<A HREF="http://www.google.com/">here</A>.\r\n"
  17. ,"<A HREF="http://www.google.com/">here</A>.\r\n"
  18. ,"<A HREF="http://www.google.com/">here</A>.\r\n"
  19. ,"</BODY></HTML>\r\n");
  20. $hp = new HttpParser();
  21. foreach($buffs as $buff){
  22. $ret = $hp->execute($buff);
  23. if($ret !== false){
  24. echo $ret;
  25. break;
  26. }
  27. }
虽然http请求可能分包发送,HttpParser会将所有包合并在一起后,出发body事件,然后调用相应的回调方法。 诸如header回调,目前暂未实现。另外,此处需要自行实现timeout逻辑。

示例代码是结合swoole_client与swPromise框架实现的一个异步http client。 籍此可以实现真正的非阻塞的PHP程序。
  1. class HttpClientFuture implements FutureIntf {
  2. protected $url = null;
  3. protected $post = null;
  4. protected $proxy = false;
  5. public function __construct($url, $post = array(), $proxy = array()) {
  6. $this->url = $url;
  7. $this->post = $post;
  8. if($proxy){
  9. $this->proxy = $proxy;
  10. }
  11. }
  12. public function run(Promise &$promise) {
  13. $cli = new \swoole_client ( SWOOLE_TCP, SWOOLE_SOCK_ASYNC );
  14. $urlInfo = parse_url ( $this->url );
  15. if(!isset($urlInfo ['port']))$urlInfo ['port'] = 80;
  16. $httpParser = new \HttpParser();
  17. $cli->on ( "connect", function ($cli)use($urlInfo){
  18. $host = $urlInfo['host'];
  19. if($urlInfo['port'])$host .= ':'.$urlInfo['port'];
  20. $req = array();
  21. $req[] = "GET {$this->url} HTTP/1.1\r\n";
  22. $req[] = "User-Agent: PHP swAsync\r\n";
  23. $req[] = "Host:{$host}\r\n";
  24. $req[] = "Connection:close\r\n";
  25. $req[] = "\r\n";
  26. $req = implode('', $req);
  27. $cli->send ( $req );
  28. } );
  29. $cli->on ( "receive", function ($cli, $data = "") use(&$httpParser, &$promise) {
  30. $ret = $httpParser->execute($data);
  31. if($ret !== false){
  32. $cli->close();
  33. $promise->accept(['http_data'=>$ret]);
  34. }
  35. } );
  36. $cli->on ( "error", function ($cli) use(&$promise) {
  37. $promise->reject ();
  38. } );
  39. $cli->on ( "close", function ($cli) {
  40. } );
  41. if($this->proxy){
  42. $cli->connect ( $this->proxy['host'], $this->proxy ['port'], 1 );
  43. }else{
  44. $cli->connect ( $urlInfo ['host'], $urlInfo ['port'], 1 );
  45. }
  46. }
  47. }
性能
  1. [web@gz-web01 php_http_parser]$ time /data/server/php/bin/php http_parser.php 2000000
  2. real 0m11.489s
  3. user 0m11.435s
  4. sys 0m0.017s
1个worker进程
  1. ./http_load -fetches 20000 -parallel 100 9502.listasync
  2. 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 5.94536 seconds
  3. 101 mean bytes/connection
  4. 3363.97 fetches/sec, 339761 bytes/sec
  5. msecs/connect: 0.0473873 mean, 1.155 max, 0.019 min
  6. msecs/first-response: 29.6366 mean, 51.736 max, 15.22 min
  7. HTTP response codes:
  8. code 200 -- 20000
  9. -bash: history: write error: Success
2个worker进程
  1. ./http_load -fetches 20000 -parallel 100 9502.listasync
  2. 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 3.17119 seconds
  3. 101 mean bytes/connection
  4. 6306.77 fetches/sec, 636984 bytes/sec
  5. msecs/connect: 0.0643583 mean, 1.211 max, 0.023 min
  6. msecs/first-response: 15.7489 mean, 32.425 max, 3.242 min
  7. HTTP response codes:
  8. code 200 -- 20000
  9. -bash: history: write error: Success
4个woker进程
  1. ./http_load -fetches 20000 -parallel 100 9502.listasync
  2. 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 1.57194 seconds
  3. 101 mean bytes/connection
  4. 12723.2 fetches/sec, 1.28504e+06 bytes/sec
  5. msecs/connect: 0.0815263 mean, 1.349 max, 0.02 min
  6. msecs/first-response: 7.65904 mean, 22.568 max, 1.221 min
  7. HTTP response codes:
  8. code 200 -- 20000
  9. -bash: history: write error: Success
8个woker进程
  1. ./http_load -fetches 20000 -parallel 100 9502.listasync
  2. 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 1.02967 seconds
  3. 101 mean bytes/connection
  4. 19423.8 fetches/sec, 1.9618e+06 bytes/sec
  5. msecs/connect: 0.147502 mean, 1.575 max, 0.014 min
  6. msecs/first-response: 3.17218 mean, 22.566 max, 0.339 min
  7. HTTP response codes:
  8. code 200 -- 20000
  9. -bash: history: write error: Success
"小礼物走一走,来肥宅自学平台支持我"
评论区

评论

共条评论
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~
客服中心在线客服
全心全意为平台用户服务
Copyright © 2017-2023 自学平台网站地图:去查看>
  • 肥宅人自己的自学平台
  • 人人都能成为平台讲师
  • 贡献你的力量壮大肥宅自学平台
  • 将自己的能力变现
  • 实现人生的第二职业
网址收藏平台安卓APP
微信公众号微信公众号

本站部分图片或者资源来自程序自动采集或卖家(商家)发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!

肥宅自学教程网是一个主打IT视频教程、自媒体运行、摄影剪辑等内容的资源学习交流平台。