国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

淺析關(guān)于PHP中Sphinx長連接問題

2024-05-04 21:48:27
字體:
供稿:網(wǎng)友

關(guān)于什么是Sphinx這里我不不介紹了大家可百度查一下,下面我來介紹的是關(guān)于PHP中Sphinx長連接問題解析,希望些文章對各位朋友有幫助.

SphinxClient::open

(PECL sphinx >= 1.0.3)

SphinxClient::open — 建立到搜索服務(wù)端的持久連接

說明:public bool SphinxClient::open(void)

建立到搜索服務(wù)端的持久連接.

參數(shù):此函數(shù)沒有參數(shù).

返回值:成功時返回 TRUE,或者在失敗時返回 FALSE.

今日在做PHP系統(tǒng)代碼優(yōu)化時,對sphinx的長連接做了一些分析發(fā)現(xiàn)php的sphinx api并不是我們想象中的那樣會在php-fpm的fastcgi狀態(tài)下一直與sphinx的searchd進(jìn)程保持長連接,sphinx的api接口中open()方法僅僅提供了在一次會話請求中保證多個sphinx調(diào)用在單個php進(jìn)程中是共用一個sphinx tcp連接通道,當(dāng)php解釋運(yùn)行完,與sphinx的連接也會自動斷開,而不是保持連接狀態(tài).

  1. > So it seems that the definition of 'persistent connection' in Sphinx is different from 
  2. > persistent MySql connections when using a PhP API : the persistence is only across 
  3. > multiple calls *in the same php request execution* and not persistence within the client 
  4. > process i.e. across multiple php requests. 

我們可以做一個這樣的實(shí)驗(yàn)來證明我的觀點(diǎn),給php增加sphinx.so擴(kuò)展,然后寫如下測試代碼:

  1.  
  2. $s = new SphinxClient(); 
  3. //開源代碼Vevb.com 
  4. var_dump($s); 
  5. $s->setServer('192.168.1.108','9312'); 
  6. //$s->open(); 
  7. var_dump($s->query('abxxxx')); 
  8. var_dump($s->query('abxxxx')); 

注意這里$s->open()先屏蔽,然后我們在cli狀態(tài)下利用strace命令跟蹤執(zhí)行此php腳本,收集系統(tǒng)調(diào)用信息會發(fā)現(xiàn).

在系統(tǒng)調(diào)用中出現(xiàn)了兩次connect到192.168.1.108的請求,也就是說在沒調(diào)用open方法的時候,在同一個php運(yùn)行時中會導(dǎo)致兩次對sphinx產(chǎn)生的tcp請求.

  1. 611 fcntl64(3, F_SETFL, O_RDONLY|O_NONBLOCK) = 0 
  2. 612 connect(3, {sa_family=AF_INET, sin_port=htons(9312), sin_addr=inet_addr("192.168.1.108")}, 16) = -1 EINPROGRESS (Operation now in progress) 
  3. 613 select(4, NULL, [3], NULL, {60, 0})     = 1 (out [3], left {59, 999996}) 
  4. 614 fcntl64(3, F_SETFL, O_RDONLY)           = 0 
  5. 615 send(3, "1", 4, MSG_NOSIGNAL)    = 4 
  6. 616 recv(3, "1", 4, 0)               = 4 
  7. 617 send(3, "1312241", 16, MSG_NOSIGNAL) = 16 
  8. 618 send(3, "246abxx"..., 140, MSG_NOSIGNAL) = 140 
  9. 619 recv(3, "131`", 8, 0)       = 8 
  10. 620 recv(3, "25title4text2"..., 96, 0) = 96 
  11. 621 close(3)     
  12. 。。。。。。。。。。。。。。。。。。。 
  13. 。。。。。。。。。。。。。。。。。。。 
  14. 756 socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3 
  15. 757 fcntl64(3, F_SETFL, O_RDONLY|O_NONBLOCK) = 0 
  16. 758 connect(3, {sa_family=AF_INET, sin_port=htons(9312), sin_addr=inet_addr("192.168.1.108")}, 16) = -1 EINPROGRESS (Operation now in progress) 
  17. 759 select(4, NULL, [3], NULL, {60, 0})     = 1 (out [3], left {59, 999997}) 
  18. 760 fcntl64(3, F_SETFL, O_RDONLY)           = 0 
  19. 761 send(3, "1", 4, MSG_NOSIGNAL)    = 4 
  20. 762 recv(3, "1", 4, 0)               = 4 
  21. 763 send(3, "1312241", 16, MSG_NOSIGNAL) = 16 
  22. 764 send(3, "246abxx"..., 140, MSG_NOSIGNAL) = 140 
  23. 765 recv(3, "131`", 8, 0)       = 8 
  24. 766 recv(3, "25title4text2"..., 96, 0) = 96 
  25. 767 close(3)                                = 0 
  26. 768 write(1, "array(9) {n", 11array(9) { 

然后我們?nèi)∠鹢pen調(diào)用的注釋,繼續(xù)strace,會發(fā)現(xiàn)這時候依然是連續(xù)調(diào)用兩次query方法,但在第一次query調(diào)用后api不會立即close掉tcp連接,而是繼續(xù)給到第二次query調(diào)用使用.

  1. 611 fcntl64(3, F_SETFL, O_RDONLY|O_NONBLOCK) = 0 
  2. 612 connect(3, {sa_family=AF_INET, sin_port=htons(9312), sin_addr=inet_addr("192.168.1.108")}, 16) = -1 EINPROGRESS (Operation now in progress) 
  3. 613 select(4, NULL, [3], NULL, {60, 0})     = 1 (out [3], left {59, 999996}) 
  4. 614 fcntl64(3, F_SETFL, O_RDONLY)           = 0 
  5. 615 send(3, "1", 4, MSG_NOSIGNAL)    = 4 
  6. 616 recv(3, "1", 4, 0)               = 4 
  7. 617 send(3, "441", 12, MSG_NOSIGNAL) = 12 
  8. 618 select(4, [3], NULL, [3], {0, 0})       = 0 (Timeout) 
  9. 619 send(3, "1312241", 16, MSG_NOSIGNAL) = 16 
  10. 620 send(3, "246abxx"..., 140, MSG_NOSIGNAL) = 140 
  11. 621 recv(3, "131`", 8, 0)       = 8 
  12. 622 recv(3, "25title4text2"..., 96, 0) = 96 
  13. 623 write(1, "array(9) {n", 11array(9) { 
  14. 624 )    = 11 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 重庆市| 鄂尔多斯市| 聂拉木县| 张北县| 绥江县| 荆门市| 东源县| 河曲县| 南充市| 临城县| 厦门市| 宁远县| 大邑县| 图木舒克市| 洛阳市| 嵊泗县| 旌德县| 黑龙江省| 鄂尔多斯市| 友谊县| 自贡市| 惠水县| 延寿县| 清流县| 高青县| 九龙坡区| 吕梁市| 岳阳市| 江油市| 铅山县| 行唐县| 寿宁县| 石门县| 建德市| 枣庄市| 静宁县| 昭通市| 石棉县| 衡东县| 永泰县| 镇江市|