Try calling Python from Ruby with thrift

install

mac


brew install thrift # thrift
gem install thrift # ruby
pip install thrift # python

example

Refer to https://www.ibm.com/developerworks/jp/opensource/library/os-cloud-apache-thrift/

Note: Try with Python 2 first

Do it almost according to the page

definition of thrift file

proj.thrift


# proj.thrift

namespace py demoserver
namespace rb demoserver

/* All operands are 32-bit integers called a Value */
typedef i32 Value
typedef i32 Result

/* Math service exposes an some math function */
service MyMath
{
  Result add( 1: Value op1, 2: Value op2 ),
  Result mul( 1: Value op1, 2: Value op2 ),
  Result min( 1: Value op1, 2: Value op2 ),
  Result max( 1: Value op1, 2: Value op2 )
}

Create Ruby and Python files

thrift --gen py proj.thrift
thrift --gen rb proj.thrift

Create server.py

Write the processing part in this

server.py


#!/usr/bin/python

import sys

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

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

class MathImpl( demoserver.MyMath.Iface ):
    def add( self, op1, op2 ):
        return op1 + op2
    def mul( self, op1, op2 ):
        return op1 * op2
    def max( self, op1, op2 ):
        return max([op1, op2])
    def min( self, op1, op2 ):
        return min([op1, op2])

if __name__ == '__main__':

    processor = demoserver.MyMath.Processor( MathImpl() )
    transport = TSocket.TServerSocket( port = 18181 )
    tbfactory = TTransport.TBufferedTransportFactory()
    pbfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TThreadedServer( processor, transport, tbfactory, pbfactory )

    print('Starting the Math Server...')

    server.serve();

write ruby client

client.rb


# Make thrift-generated code visible
$:.push('./gen-rb')

require 'thrift'
require 'my_math'

begin

    # Build up the Thrift stack
    transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', 18181))
    protocol = Thrift::BinaryProtocol.new(transport)
    client = Demoserver::MyMath::Client.new(protocol)
    transport.open()

    # Try an add operation
    result = client.add( 1, 5 )
    puts result.inspect

    # Try a max operation
    result = client.max( 9, 7 )
    puts result.inspect

    transport.close()

Execute

In the reference link

python server.py
ruby client.rb
6 ## 1+5 answers
9 ##Maximum answer for 9 and 7

Recommended Posts

Try calling Python from Ruby with thrift
Try scraping with Python.
Try Python output with Haxe 3.2
Try running Python with Try Jupyter
Try face recognition with Python
With skype, notify with skype from python!
I tried using the Python library from Ruby with PyCall
Try scraping with Python + Beautiful Soup
Scraping with Node, Ruby and Python
Using Rstan from Python with PypeR
Install Python from source with Ansible
Create folders from '01' to '12' with python
Dynamic proxy with python, ruby, PHP
Try to operate Facebook with Python
Try singular value decomposition with Python
Call popcount from Ruby / Python / C #
Run Aprili from Python with Orange
Try face recognition with python + OpenCV
Call python from nim with Nimpy
Try frequency control simulation with Python
Read fbx from python with cinema4d
Try using Amazon DynamoDB from Python
Try python
[Python] Try to recognize characters from images with OpenCV and pyocr
Collecting information from Twitter with Python (Twitter API)
Try to reproduce color film with Python
Receive textual data from mysql with python
Get html from element with Python selenium
Try logging in to qiita with Python
[Note] Get data from PostgreSQL with Python
Play audio files from Python with interrupts
Create wordcloud from your tweet with python3
Try mathematical formulas using Σ with python
Try working with binary data in Python
Tweet from python with Twitter Developer + Tweepy
Note for Pyjulia calling Julia from Python
Try using Python with Google Cloud Functions
Business efficiency starting from scratch with Python
Decrypt files encrypted with openssl from python with openssl
Working with Azure CosmosDB from Python Part.2
Image acquisition from camera with Python + OpenCV
Getting started with Dynamo from Python boto
Try HTML scraping with a Python library
Scraping from an authenticated site with python
Encrypt with Ruby (Rails) and decrypt with Python
Use C ++ functions from python with pybind11
Easy web scraping with Python and Ruby
Try drawing a map with python + cartopy 0.18.0
Try something like Python for-else in Ruby
[Continued] Try PLC register access with Python
Try assigning or switching with Python: lambda
[For beginners] Try web scraping with Python
The road to compiling to Python 3 with Thrift
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Collecting information from Twitter with Python (Environment construction)
nginxparser: Try parsing nginx config file with Python
FizzBuzz with Python3
Csv output from Google search with [Python]! 【Easy】
Scraping with Python
Try to beautify with Talking Head Anime from a Single Image [python preparation]
How to enjoy programming with Minecraft (Ruby, Python)