3

这实际上是我关于 stackoverflow 的第一个问题,但我一直遇到一个我似乎无法真正解决的问题。我正在制作一个通过 thrift 调用 erlang 客户端的 Python 服务器。我在 thrift 中创建的唯一函数是一个叫做 bar 的函数,它接收一个整数并打印 bar(整数)。这是 Python 客户端,它并不太复杂:

#!/usr/bin/env python

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

from foo import Foo
from foo.ttypes import *
from foo.constants import *

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

try:
   # Make socket
   transport = TSocket.TSocket('localhost', 9999)

   # Buffering is critical. Raw sockets are very slow
   transport = TTransport.TBufferedTransport(transport)

   # Wrap in a protocol
   protocol = TBinaryProtocol.TBinaryProtocol(transport)

   # Create a client to use the protocol encoder
   client = Foo.Client(protocol)

   # Connect!
   transport.open()

   msg = client.bar(1452)
   print msg

   transport.close()

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

这是我的 thrift 客户端,它正在侦听端口 9999:

-module(foo_service).

-include("foo_thrift.hrl").
-include("foo_types.hrl").

-export([start_link/0, stop/1,
         handle_function/2,

% Thrift implementations
% FILL IN HERE
         bar/1]).

%%%%% EXTERNAL INTERFACE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

start_link() ->
  thrift_socket_server:start ([{port, get_port()},
                               {name, ?MODULE},
                               {service, foo_thrift},
                               {handler, ?MODULE},
                               {framed, true},
                               {socket_opts, [{recv_timeout, 60*60*1000}]}]).

stop(_Server) ->
  thrift_socket_server:stop (?MODULE),
  ok.

%%%%% THRIFT INTERFACE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

handle_function(Function, Args) when is_atom(Function), is_tuple(Args) ->
  case apply(?MODULE, Function, tuple_to_list(Args)) of
    ok -> ok;
    Reply -> {reply, Reply}
  end.

%%%%% HELPER FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

get_port() ->
  {ok, Result} = application:get_env(foo, service_port),
  Result.

%% ADD THRIFT FUNCTIONS HERE
bar(I) ->
  io:format("bar (~p)~n", [I]).

所以我启动了 thrift 客户端,并从 python 服务器调用了 client.bar(1452),不幸的是得到了一个子错误:

=CRASH REPORT==== 5-Jul-2013::08:34:32 ===
  crasher:
    initial call: thrift_socket_server:acceptor_loop/1
    pid: <0.51.0>
    registered_name: []
    exception error: no function clause matching 
                     thrift_socket_transport:read({data,#Port<0.1067>,3600000},
                                                  -2147418111) (src/thrift_socket_transport.erl, line 53)
      in function  thrift_transport:read/2 (src/thrift_transport.erl, line 67)
      in call from thrift_framed_transport:read/2 (src/thrift_framed_transport.erl, line 79)
      in call from thrift_transport:read/2 (src/thrift_transport.erl, line 67)
      in call from thrift_binary_protocol:read_data/2 (src/thrift_binary_protocol.erl, line 315)
      in call from thrift_binary_protocol:read/2 (src/thrift_binary_protocol.erl, line 286)
      in call from thrift_binary_protocol:read/2 (src/thrift_binary_protocol.erl, line 175)
      in call from thrift_protocol:read_specific/2 (src/thrift_protocol.erl, line 186)
    ancestors: [foo_service,foo_sup,<0.46.0>]
    messages: []
    links: [<0.48.0>,#Port<0.1067>]
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 987
    stack_size: 27
    reductions: 513
  neighbours:

=ERROR REPORT==== 5-Jul-2013::08:34:32 ===
{thrift_socket_server,244,{child_error,function_clause,[]}}

有任何想法吗?谢谢你的帮助!

4

1 回答 1

4

弄清楚了!当我在我的 erlang 文件中指定帧传输时,我正在使用 TBufferedTransport。我将其更改为 TFramedTrasport,重新编译了我的 thrift 文件,一切正常。

于 2013-07-06T23:15:10.250 回答