rryqszq4 / ngx-php

ngx-php - Embedded php7 or php8 scripting language for nginx module. Mainline development version of the ngx-php.
BSD 2-Clause "Simplified" License
639 stars 55 forks source link

有办法优化ngx_php7把那些yield去掉变成跟ngx_lua一样的写法么? #5

Open diyism opened 6 years ago

diyism commented 6 years ago

有办法优化ngx_php7把那些yield去掉变成跟ngx_lua一样的写法么?

content_by_php '
    yield ngx_socket::connect("hq.sinajs.cn", 80);
    yield ngx_socket::send("GET /list=s_sh000001 HTTP/1.0\r\n
                            Host: hq.sinajs.cn\r\nConnection: close\r\n\r\n");
    yield $ret = ngx_socket::recv(1024);
    yield ngx_socket::close();
    var_dump($ret);
';

变成:

content_by_php '
    ngx_socket::connect("hq.sinajs.cn", 80);
    ngx_socket::send("GET /list=s_sh000001 HTTP/1.0\r\n
                            Host: hq.sinajs.cn\r\nConnection: close\r\n\r\n");
    $ret = ngx_socket::recv(1024);
    ngx_socket::close();
    var_dump($ret);
';
rryqszq4 commented 6 years ago

@diyism 这是一个很有意思的提问,由于php底层设计与lua不一样,没有办法像lua那样保存上下文状态。目前才使用yield和yield from来实现非阻塞,这样既不会阻塞zend引擎也不会阻塞nginx,可以使php和nginx很好的写作,当然目前的api还有待优化。

另外如果想优化掉yield使其变的优雅,我之前想过两个方案:

  1. 在php脚本编译期做一个伪替换,ngx_socket 替换为 yield ngx_socket
  2. 使用hack方法,在zend引擎底层使用协程,难度很大
diyism commented 6 years ago

觉得第1种方法容易些,在nginx把php代码扔给zend之前替换下,等着好消息。另外觉得可以搞个捐款人排行榜页面,捐款人都列上面,鼓励大家来给这个项目捐款

rryqszq4 commented 6 years ago

@diyism😄

cg-huskiai commented 3 years ago

swow这个扩展可以看看行不,做好了就是openresty

joanhey commented 3 years ago

Is open and work really good !!