ThriftPyを使って作ったサーバーを動かすのに、thriftpy本体が持ってるサーバークラスを使ってもいいのだけど、タイムアウト処理やマルチプロセス動作だとかやるのが少し面倒だなーと考えてました。
githubでサーバークラスを差し替えるpull requestを出してたら、コメントでgunicorn_thriftというパッケージがあるから試すといいよと教わりました。ThriftPyと同じOrganization。
github.com
ThriftPyで作ったサービスの場合は、TProcessorのインスタンスをgunicorn_thriftコマンドに指定して起動する。
Python3.5、Ubuntu 16.04で試した。
main.py:
import thriftpy
from thriftpy.thrift import TProcessor
class AppServiceHandler:
def echo(self, message):
return message
main_thrift = thriftpy.load("main.thrift")
app = TProcessor(main_thrift.TIAppService, AppServiceHandler())
起動コマンド:
$ gunicorn_thrift -w 4 main:app
裏では、gunicornをカスタムのワーカークラスが動作している。gunicornのコマンドオプションも使えてうれしいです。
thrift_syncクラスを使った場合、単純なロードのパフォーマンスはmake_serverのほうがほんの少し良いみたいですが、気にするほどではなさそう。
以下、パフォーマンス試験に使ったコードと結果
thriftpy performance test
ありがたい。