问题
I am developing a small application with RPC framework Apache Thrift and Python. I want to use MongoDB. I am using mongoengine connector and I defined thrift data model and generated code from it
When storing tags in a list in the server all is fine. However, I get errors when trying to store tags in database
Here Is my code
server.py
import os
import sys
from mongoengine import *
sys.path.append(os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'gen-py'))
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from tag import *
class Tag(Document):
identifier = LongField()
description = StringField()
class TagHandler(object):
#def __init__(self):
#self.tags = []
def createTag(self, identifier, description):
tag = Tag(identifier=identifier, description=description).save()
#self.tags.append(tag)
return tag
connect("tags", host="127.0.0.1")
handler = TagHandler()
processor = TagService.Processor(handler)
transport = TSocket.TServerSocket('127.0.0.1', 9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print "Starting python server..."
server.serve()
tag.thrift
exception TagException {
1: string Reason
}
service TagService {
i64 createTag(1: i64 identifier, 2: string description) throws (1:TagException e)
}
client.py
host = '127.0.0.1'
port = 9090
import sys
import os
sys.path.append(os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'gen-py'))
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from tag import *
transport = TSocket.TSocket( host , port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = TagService.Client(protocol)
transport.open()
result = client.createTag(12345, "dirty creation")
print ("success")
transport.close()
When executing this, I get
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes
in the client and
No handlers could be found for logger "thrift.server.TServer"
in the server
Any help will be highly appreciated
回答1:
You should be careful with your :
i64 createTag(1: i64 identifier, 2: string description) throws (1:TagException e)
Should be better :
void createTag(1: i64 identifier, 2: string description) throws (1:TagException e)
Care, you have to be ok with that until next Tuesday. lol.
来源:https://stackoverflow.com/questions/48874003/tsocket-read-0-bytes-apache-thrift-mongodb