Home  >  Article  >  Backend Development  >  PHP communicates with Golang

PHP communicates with Golang

PHPz
PHPzOriginal
2017-04-04 14:40:471685browse

A scene I encountered recently: phpThe project needs to use a third-party function (stammering word segmentation) , and github happens to have a class library written in Golang. Then the question arises, how to achieve communication between different languages. What about?

Conventional solution:

  • ##Write an http/TCP service in Golang, and php communicates with Golang through http/TCP

  • Encapsulate Golang as a php extension

  • PHP calls the Golang executable file through system commands

Existing problems:

  • http request, network I/O will consume a lot of time

  • Need to encapsulate a lot of code

  • Every time PHP calls a Golang program, it needs to be initialized once, which consumes a lot of time.

Optimization goal:

  • The Golang program is only initialized once (because initialization is time-consuming)

  • All requests do not need to go through the network

  • Try not to modify the code a lot

Solution:

  • Simple Golang encapsulation, compile the third-party class library into an executable file

  • PHP and Golang communicate through two-way pipelines

Advantages of using two-way pipeline communication:

1: Only a small amount of encapsulation of the original Golang class library is required

2: Best performance (IPC communication is the best way to communicate between processes)
3: No network requests are required, saving a lot of time
4: The program only needs to be initialized once and remains in memory.

Specific implementation steps:

  • 1: Original call demo in the class library

          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, "/"))
          }

    Save the file as main.go, just Can run

  • 2: The adjusted code is:

          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")
                  }
              }
          }
    It only takes a few simple adjustments to achieve: receiving from standard input

    String , after word segmentation, output
    Test:

      # go build test
      # ./test
      # //等待用户输入,输入”这是一个测试“
      # 这是 一个 测试 //程序
  • 3: Use cat to communicate with Golang for a simple test

      //准备一个title.txt,每行是一句文本
      # cat title.txt | ./test
    Normal output, indicating that cat can interact with Golang normally

  • 4: PHP communicates with Golang


    The communication between cat and Golang shown above uses a single to the pipeline. That is: data can only be passed from cat to Golang. The data output by Golang is not passed back to cat, but is output directly to the screen. But the requirement in the article is: php communicates with Golang. That is, php needs to pass data to Golang, and Golang must also return the execution results to php. Therefore, a bidirectional pipeline needs to be introduced.
    The use of pipes in PHP: popen("/path/test"), I won’t go into details because this method cannot solve the problem in the article.
    Bidirectional pipe:

          $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]);

    Explanation: Use proc_open to open a process and call the Golang program. At the same time, a bidirectional pipeline pipesarray is returned. PHP writes data to $pipe['0'] and reads data from $pipe['1'].

Okay, maybe you have discovered that I am the title file, and the focus here is not just how PHP and Golang communicate. Instead, we are introducing a method:

Let any language communicate through a two-way pipe. (All languages ​​will implement pipeline related content)

Test:

Through

comparison test, calculate the time occupied by each process. The title.txt file mentioned below contains 1 million lines of text. Each line of text is the product title taken from the b2b platform

1: The overall process takes time


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

## Time consuming: 14.819 seconds, including:

    Process cat reads out text
  • Transmit data into Golang through pipes
  • Golang processes the data and returns the results to the screen
#2: It takes time to calculate the word segmentation
function

. Solution: Remove the call to the word segmentation function, that is: CommentRemove the line of code in the Golang source code that calls the word segmentation functiontime cat title.txt | ./test > /dev /null
## Time consuming: 1.817 seconds, the time consumed includes:

Process cat reads text
  • Pass data into Golang through pipes
  • Golang processes the data
  • and returns the results to the screen

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

3:测试cat进程与Golang进程之间通信所占时间
time cat title.txt > /dev/null

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

  • 进程cat读出文本

  • 通过管道将数据传入Golang

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

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

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

         array("pipe", "r"), 
                1 => array("pipe", "w")
            );

            $handle = proc_open(
                '/webroot/go/src/test/test', 
                $descriptorspec, 
                $pipes
            );

            $fp = fopen("title.txt", "rb");

            while (!feof($fp)) {
                fwrite($pipes['0'], trim(fgets($fp))."\n");
                echo fgets($pipes[1]);
            }

            fclose($pipes['0']);
            fclose($pipes['1']);
            proc_close($handle);

流程与上面基本一致,读出title.txt内容,通过双向管道传入Golang进程分词后,再返回给php (比上面的测试多一步:数据再通过管道返回)
time php popen.php > /dev/null

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

  • 进程PHP读出文本

  • 通过管道将数据传入Golang

  • Golang处理数据

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

  • 将结果返回到屏幕

结论:

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

使用cat控制逻辑耗时:        14.819 秒
使用PHP控制逻辑耗时:         24.037 秒(比cat多一次管道通信)
单向管道通信耗时:           1.8    秒
Golang中的分词函数耗时:     13.002 秒

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

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

相关问题:

  • 1:以上Golang源码中写的是一个循环,也就是会一直从管道中读数据。那么存在一个问题:是不是php进程结束后,Golang的进程还会一直存在?

    管道机制自身可解决此问题。管道提供两个接口:读、写。当写进程结束或者意外挂掉时,读进程也会报错,以上Golang源代码中的err逻辑就会执行,Golang进程结束。
    但如果PHP进程没有结束,只是暂时没有数据传入,此时Golang进程会一直等待。直到php结束后,Golang进程才会自动结束。

  • 2:能否多个php进程并行读写同一个管道,Golang进程同时为其服务?

    不可以。管道是单向的,如果多个进程同时向管道中写,那Golang的返回值就会错乱。
    可以多开几个Golang进程实现,每个php进程对应一个Golang进程。

The above is the detailed content of PHP communicates with Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn