Khatri
Khatri

Reputation: 636

How to pass a thrift struct to Python Server

I'm using thrift for rpc communication between python server and java client. In my thrift Handler I've one function where I want to pass a thrift struct. For basic data type passing I can directly do:

def assignValue(self, variable, value):

but now instead of variable (which is of string type) I want to pass around struct. How can I do that?

struct MyStruct { 1:string var1; 2:string var2; }

PS: I'm new to thrift and I'm following thrift's official documentation, If there is any other document which can be helpful, please feel free to pass around that. Thanks!

Upvotes: 1

Views: 1750

Answers (1)

codeSF
codeSF

Reputation: 1172

Here's a simple example passing a struct in Thrift IDL:

https://github.com/RandyAbernethy/ThriftBook/blob/master/part1/arch/halibut.thrift

struct Date {
    1: i16  year
    2: i16  month
    3: i16  day
}

service HalibutTracking {
    i32 get_catch_in_pounds_today()
    i32 get_catch_in_pounds_by_date(1: Date dt, 2: double tm)
}

There's a Java client and server example here:

https://github.com/RandyAbernethy/ThriftBook/tree/master/part3/ws/thrift

It shows returning a struct (but passing one is pretty easy to extrapolate). There are also many examples in the ThriftBook repo in Python and Java (all source from the Programmer's Guide to Apache Thrift):

The example struct return thrift idl looks like:

struct TradeReport {
    1: string  symbol,
    2: double  price,
    3: i32     size,
    4: i32     seq_num
} 

service TradeHistory {
    TradeReport get_last_sale(1: string Symbol) 
}

The Java Client in the listing looks like this:

import java.io.IOException;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.protocol.TBinaryProtocol;

public class ThriftClient {
    public static void main(String[] args) 
            throws IOException, TException {
        TSocket trans = new TSocket("localhost", 9090);
        TBinaryProtocol proto = new TBinaryProtocol(trans);
        TradeHistory.Client client = new TradeHistory.Client(proto);

        trans.open();
        for (int i = 0; i < 1000000; i++) {
            TradeReport tr = client.get_last_sale("APPL");
        }
        trans.close();
    }
}

Other IDL examples are here (including several that pass structs):

https://github.com/RandyAbernethy/ThriftBook/tree/master/part2/IDL

Upvotes: 2

Related Questions