最近遇到的一个场景:php项目中需要使用一个第三方的功能(结巴分词),而github上面恰好有一个用Golang写好的类库。那么问题就来了,要如何实现不同语言之间的通信呢?

常规的方案:

  • 用Golang写一个http/TCP服务,php通过http/TCP与Golang通信

  • 将Golang经过较多封装,做为php扩展。

  • PHP通过系统命令,调取Golang的可执行文件

存在的问题:

  • http请求,网络I/O将会消耗大量时间

  • 需要封装大量代码

  • PHP每调取一次Golang程序,就需要一次初始化,时间消耗很多

优化目标:

  • Golang程序只初始化一次(因为初始化很耗时)

  • 所有请求不需要走网络

  • 尽量不大量修改代码

解决方案:

  • 简单的Golang封装,将第三方类库编译生成为一个可执行文件

  • PHP与Golang通过双向管道通信

使用双向管道通信优势:

1:只需要对原有Golang类库进行很少的封装
2:性能最佳 (IPC通信是进程间通信的最佳途径)
3:不需要走网络请求,节约大量时间
4:程序只需初始化一次,并一直保持在内存中

具体实现步骤:

      package main
      import (
          "fmt"
          "github.com/yanyiwu/gojieba"
          "strings"
      )

      func main() {
          x := gojieba.NewJieba()
          defer x.Free()

          s := "小明硕士毕业于中国科学院计算所,后在日本京都大学深造"
          words := x.CutForSearch(s, true)
          fmt.Println(strings.Join(words, "/"))
      }
      package main
      import (
          "bufio"
          "fmt"
          "github.com/yanyiwu/gojieba"
          "io"
          "os"
          "strings"
      )

      func main() {

          x := gojieba.NewJieba(
              "/data/tmp/jiebaDict/jieba.dict.utf8", 
              "/data/tmp/jiebaDict/hmm_model.utf8", 
              "/data/tmp/jiebaDict/user.dict.utf8"
          )
          defer x.Free()

          inputReader := bufio.NewReader(os.Stdin)
          for {
              s, err := inputReader.ReadString('\n')
              if err != nil && err == io.EOF {
                  break
              }
              s = strings.TrimSpace(s)

              if s != "" {
                  words := x.CutForSearch(s, true)
                  fmt.Println(strings.Join(words, " "))
              } else {
                  fmt.Println("get empty \n")
              }
          }
      }
  # go build test
  # ./test
  # //等待用户输入,输入”这是一个测试“
  # 这是 一个 测试 //程序
  //准备一个title.txt,每行是一句文本
  # cat title.txt | ./test
      $descriptorspec = array( 
          0 => array("pipe", "r"), 
            1 => array("pipe", "w")
      );
      $handle = proc_open(
          '/webroot/go/src/test/test', 
          $descriptorspec, 
          $pipes
      );
      fwrite($pipes['0'], "这是一个测试文本\n");
      echo fgets($pipes[1]);

好吧,也许你已经发现,我是标题档,这里重点要讲的并不只是PHP与Golang如何通信。而是在介绍一种方法: 通过双向管道让任意语言通信。(所有语言都会实现管道相关内容)

测试:

通过对比测试,计算出各个流程占用的时间。下面提到的title.txt文件,包含100万行文本,每行文本是从b2b平台取的商品标题

time cat title.txt | ./test > /dev/null

耗时:14.819秒,消耗时间包含:

  • 进程cat读出文本

  • 通过管道将数据传入Golang

  • Golang处理数据,将结果返回到屏幕

time cat title.txt | ./test > /dev/null

耗时:1.817秒时间,消耗时间包含:

  • 进程cat读出文本

  • 通过管道将数据传入Golang

  • Golang处理数据,将结果返回到屏幕

分词耗时 = (第一步耗时) - (以上命令所耗时)
分词耗时 : 14.819 - 1.817 = 13.002秒

time cat title.txt > /dev/null

耗时:0.015秒,消耗时间包含:

  • 进程cat读出文本

  • 通过管道将数据传入Golang

  • go处理数据,将结果返回到屏幕

管道通信耗时:(第二步耗时) - (第三步耗时)
管道通信耗时: 1.817 - 0.015 = 1.802秒

4:PHP与Golang通信的时间消耗
编写简单的php文件:

time php popen.php > /dev/null

耗时:24.037秒,消耗时间包含:

  • 进程PHP读出文本

  • 通过管道将数据传入Golang

  • Golang处理数据

  • Golang将返回结果再写入管道,PHP通过管道接收数据

  • 将结果返回到屏幕

结论:

1 :整个分词过程中的耗时分布

2:分词函数的性能: 单进程,100万商品标题分词,耗时13秒
以上时间只包括分词时间,不包括词典载入时间。但在本方案中,词典只载入一次,所以载入词典时间可以忽略(1秒左右)

3:PHP比cat慢 (这结论有点多余了,呵呵)
语言层面慢: (24.037 - 1.8 - 14.819) / 14.819 = 50%
单进程对比测试的话,应该不会有哪个语言比cat更快。

相关问题: