美文网首页
php和golang通信,双向管道

php和golang通信,双向管道

作者: 行走于无形之中 | 来源:发表于2019-03-24 17:20 被阅读0次

新建php文件,保存为proc.php

<?php
    $descriptorspec = array( 
          0 => array("pipe", "r"), 
          1 => array("pipe", "w")
      );

      //创建双向管道
      $handle = proc_open(
          './test_proc', 
          $descriptorspec, 
          $pipes
      );

      //写入管道 
      fwrite($pipes['0'], "123\n");
      //从管道中读取
      echo fgets($pipes[1]);

新建go文件,保存为 test_proc.go

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    //从标准输入读取数据
    inputReader := bufio.NewReader(os.Stdin)
    input,err := inputReader.ReadString('\n')
    
    //去掉\n
    input = input[0:len(input)-1]
    if err != nil {
        fmt.Println(err)
        return
    }
    
    //输出
    fmt.Println(input)
}

手动测试

#生成可执行文件 test_proc
go build  test_proc.go

echo 'hello world' | ./test_proc 
#输出hello world

php proc.php
#➜  php proc.php         
#输出 123

相关文章

  • php和golang通信,双向管道

    新建php文件,保存为proc.php 新建go文件,保存为 test_proc.go 手动测试

  • Windows进程间通信之管道

    命名管道(Named Pipe)是服务器进程和一个或多个客户进程之间通信的单向或双向管道。不同于匿名管道的是命名管...

  • 从PHP 到Golang 的笔记 ( 转 )

    ———文章来源 YamiOdymel/PHP-to-Golang 为什么从PHP 转到Golang? PHP和模块...

  • PHP 进程通信-管道

    1.简介 管道是*NIX上常见的一个东西,大家平时使用linux的时候也都在用,简单理解就是|,比如ps -aux...

  • Linux 进程间通信

    进程间通信 一 进程间通信 -- 管道 mkfifo test 创建管道文件 匿名管道和命名管道:匿名管道:匿名管...

  • golang channel的实现原理

    1. 前言 channel是Golang在语言层面提供的goroutine间的通信方式,比Unix管道更易用也更轻...

  • golang中channel的用法

    channel有点类似于管道,它在goroutine同步与通信中,有着起承转合的作用,同时也是Golang实现CS...

  • Android 进程通信--Binder机制

    一、起源——为什么在Android中使用binder通信机制? linux中的进程通信 管道包含无名管道和有名管道...

  • Linux-C-day-3-进程间通信--FIFO/套接字

    FIFIO管道 通过命名管道通信,命名管道之间的通信读和写必须同时执行,否则就会阻塞,但是命名管道可以用于非亲缘进...

  • PHP与Golang如何通信?

    最近遇到的一个场景:php项目中需要使用一个第三方的功能(结巴分词),而github上面恰好有一个用Golang写...

网友评论

      本文标题:php和golang通信,双向管道

      本文链接:https://www.haomeiwen.com/subject/zrwuvqtx.html