Skip to content

Instantly share code, notes, and snippets.

@yohm
Last active November 16, 2023 11:14
Show Gist options
  • Save yohm/339a86211b18d3c0c7ab36cb9fdedd82 to your computer and use it in GitHub Desktop.
Save yohm/339a86211b18d3c0c7ab36cb9fdedd82 to your computer and use it in GitHub Desktop.
Ruby and Python samples of MessagePack-RPC.
Gemfile.lock

A sample code of msgpack-rpc.

Setup

  • for Ruby
bundle
  • for Python
pip install msgpack-rpc-python

Run the sample

ruby server.rb

In another terminal,

ruby client.rb

Using python client,

python client.py

Call matplotlib func from Ruby

python plot_server.py
ruby plot_client.rb

Get Rails object from Python

ruby rails_server.rb
python rails_client.py
import msgpackrpc
client = msgpackrpc.Client(msgpackrpc.Address("localhost", 18800), unpack_encoding='utf-8')
future = client.call_async('delayed_add', 3, 1, 2)
print( future.get() )
require 'msgpack/rpc'
c = MessagePack::RPC::Client.new('localhost',18800)
puts "async call"
future = c.call_async(:delayed_add, 1, 1, 2)
result = future.get
puts result
puts "sync call"
result = c.call(:delayed_add, 1, 1, 2)
puts result
puts "call_async is called against a synchronous method"
future = c.call_async(:add, 1, 2)
puts future.get
import msgpackrpc
client = msgpackrpc.Client(msgpackrpc.Address("localhost", 18800), unpack_encoding='utf-8')
result = client.call('add', 1, 2)
print( result )
result = client.call('hello')
print( result )
require 'msgpack/rpc'
c = MessagePack::RPC::Client.new('localhost',18800)
result = c.call(:add, 1, 2)
puts result
puts c.call(:hello)
oacis = ENV['OACIS_ROOT']
unless oacis
$stderr.puts "set environment variable 'OACIS_ROOT'"
exit 1
end
eval_gemfile File.join( oacis, "Gemfile")
gem 'msgpack-rpc'
require 'msgpack/rpc'
c = MessagePack::RPC::Client.new('localhost',18800)
c.timeout = Float::INFINITY
xs = -3.14.step(3.14, 0.1).to_a
ys = xs.map {|x| Math.sin(x) }
c.call( :scatter, xs, ys )
import msgpackrpc
import matplotlib.pyplot as plt
import numpy as np
class MyPlot(object):
def scatter(self, x, y):
plt.scatter(x,y)
plt.show()
svr = msgpackrpc.Server( MyPlot(), unpack_encoding='utf-8' )
svr.listen( msgpackrpc.Address('localhost',18800) )
svr.start()
import msgpackrpc
client = msgpackrpc.Client(msgpackrpc.Address("localhost", 18800), unpack_encoding='utf-8')
result = client.call('get_id', 'echo')
print( result )
result = client.call('get_sim', 'echo')
print( result )
print( result['name'] )
require 'msgpack/rpc'
require File.join( ENV['OACIS_ROOT'], 'config/environment' )
Object.class_eval do
def to_msgpack(*args)
to_s.to_msgpack(*args)
end
end
class MyRailsHandler
def get_id(name)
Simulator.where(name: name).first.id
end
def get_sim(name)
Simulator.where(name: name).first.as_json
end
end
svr = MessagePack::RPC::Server.new
svr.listen('localhost', 18800, MyRailsHandler.new)
svr.run
import msgpackrpc
import threading, time
class MyHandler(object):
def add(self, x, y):
return x+y
def delayed_add(self, t, x, y):
print("delayed_add is called")
ar = msgpackrpc.server.AsyncResult()
def sleep_add():
time.sleep(t)
ar.set_result(x+y)
thread = threading.Thread(target=sleep_add)
thread.start()
return ar
def hello(self):
return "hello"
svr = msgpackrpc.Server( MyHandler(), unpack_encoding='utf-8' )
svr.listen( msgpackrpc.Address('localhost',18800) )
svr.start()
require 'msgpack/rpc'
class MyHandler
def add(x,y)
return x+y
end
def delayed_add(t,x,y)
puts "delayed_add is called"
as = MessagePack::RPC::AsyncResult.new
Thread.new do
sleep t
as.result(x+y)
end
as
end
def hello
"hello"
end
end
svr = MessagePack::RPC::Server.new
svr.listen('localhost', 18800, MyHandler.new)
svr.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment