【问题标题】:golang and python thrift communicationgolang 和 python thrift 通信
【发布时间】:2017-10-08 22:12:47
【问题描述】:

我正在使用 Thrift。 这是旧文件:

namespace py hello
namespace go hello

service Hello{ 
    string helloString(1:string para) 
} 

然后我生成 python 和 golang 代码

thrift -gen py Hello.thrift
thrift -gen go Hello.thrift 

Python 服务器代码:py-server.py

import socket
import sys
sys.path.append('./gen-py/')

from hello import Hello
from hello.ttypes import *

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

class HelloHandler:
    def helloString(self, msg):
        ret = "helloString Received: " + msg
        print ret
        return msg


handler = HelloHandler()
processor = Hello.Processor(handler)
transport = TSocket.TServerSocket("127.0.0.1", 19090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print "Starting thrift server in python..."
server.serve()

而且我可以跑得很好:

$ python py-server.py 
Starting thrift server in python...

而我的客户端是golang写的,go-client.go:

package main

import (
    "./gen-go/hello"
    "fmt"
    "git.apache.org/thrift.git/lib/go/thrift"
    "net"
    "os"
)

func main() {
    transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
    transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "19090"))
    fmt.Println(1)
    if err != nil {
        fmt.Fprintln(os.Stderr, "error resolving address:", err)
        os.Exit(1)
    }
    useTransport := transportFactory.GetTransport(transport)
    client := hello.NewHelloClientFactory(useTransport, protocolFactory)
    fmt.Println(2)
    if err := transport.Open(); err != nil {
        fmt.Println(3)
        fmt.Fprintln(os.Stderr, "localhsot:19090", " ", err)
        os.Exit(1)
    }
    defer transport.Close()
    fmt.Println(4)
    r1, e1 := client.HelloString("lalalalalalalallalaal")
    fmt.Println(5)
    fmt.Println("Call->", r1, e1)
}

但是不能运行,输出,不能正常停止

$ go run go-client.go 
1
2
4

并且服务器没有收到任何消息。

我的 golang 代码有什么问题吗?


如果我用python写一个客户端,它可以运行良好..

import socket
import sys
sys.path.append('./gen-py/')

from hello import Hello
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

try:
    transport = TSocket.TSocket('127.0.0.1', 19090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Hello.Client(protocol)
    transport.open()

    print "client - helloString"
    msg = client.helloString("lalalalalla")
    print "server - " + msg


    transport.close()

except Thrift.TException, ex:
    print "%s" % (ex.message)

$ python py-client.py 
client - helloString
server - lalalalalla

【问题讨论】:

  • Thrift 版本 0.9.2 go 版本 go1.4.2 darwin/amd64 Python 2.7.5

标签: python go thrift


【解决方案1】:

transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())

请将NewTFramedTransportFactory改为NewTBufferedTransportFactory,就像transportFactory := thrift.NewTBufferedTransportFactory(1024)一样,谢谢。

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 2017-11-26
    • 2016-09-28
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    相关资源
    最近更新 更多