Table of Contents
Conventional solution:
结论:
相关问题:
Home Backend Development PHP Tutorial PHP communicates with Golang

PHP communicates with Golang

Apr 04, 2017 pm 02:40 PM
golang php

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

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

    String , after word segmentation, output
    Test:

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

      //准备一个title.txt,每行是一句文本
      # cat title.txt | ./test
    Copy after login
    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]);
    Copy after login

    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<a href="http://www.php.cn/wiki/1268.html" target="_blank"> cat title.txt | ./test > /dev/</a>null<a href="http://www.php.cn/wiki/62.html" target="_blank"></a>

## 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文件:

        <?php
            $descriptorspec = array( 
                0 => 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);
Copy after login

流程与上面基本一致,读出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 秒
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

See all articles