msgpack-rpc-python 0.3.0 : Python Package Index
とりあえず、入れてみる。
% sudo pip-2.7 install msgpack-rpc-python Downloading/unpacking msgpack-rpc-python Running setup.py egg_info for package msgpack-rpc-python Requirement already satisfied (use --upgrade to upgrade): msgpack-python in /Library/Python/2.7/site-packages (from msgpack-rpc-python) Downloading/unpacking tornado (from msgpack-rpc-python) Running setup.py egg_info for package tornado warning: no previously-included files matching '_auto2to3*' found anywhere in distribution Installing collected packages: msgpack-rpc-python, tornado Running setup.py install for msgpack-rpc-python Running setup.py install for tornado warning: no previously-included files matching '_auto2to3*' found anywhere in distribution Successfully installed msgpack-rpc-python tornado Cleaning up... % pip-2.7 freeze |grep msgpack msgpack-python==0.1.12 msgpack-rpc-python==0.2.3
すんなり入ったのでまずサーバー側のコードを書いてみる。
% cat server.py #!/usr/bin/env python2.7 # -*- coding:utf-8 -*- import msgpackrpc class SumServer(object): """docstring for SumServer""" def __init__(self): super(SumServer, self).__init__() def sum(self, x, y): """docstring for sum""" return x**2 + y**2 if __name__ == '__main__': try: server = msgpackrpc.Server(SumServer()) server.listen(msgpackrpc.Address('localhost', 18800)) server.start() except KeyboardInterrupt, e: print e
次にクライアント側のコードを作成
% cat client.py #!/usr/bin/env python2.7 # -*- coding:utf-8 -*- import msgpackrpc client= msgpackrpc.Client(msgpackrpc.Address('localhost', 18800)) result = client.call('sum', 3,9) print result # => 90
最後に軽く動かして動作確認
% python2.7 server.py & [1] 26007 % python2.7 client.py 90
期待通りの値が返って来ました!!
このパッケージはFriendfeedが作ったtornadoをバックエンドのサーバーとして採用しているので
パフォーマンスはいいと思う。
msgpack/msgpack-rpc-python · GitHub
githubに作者測定のベンチマークあったので参考に。