├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── README.rst ├── benchmarks ├── benchmark.py ├── benchmark_msgpackrpc_official.py └── benchmark_zerorpc.py ├── cythonize.sh ├── docs ├── Makefile ├── api.rst ├── conf.py ├── img │ └── perf.png ├── index.rst ├── intro.rst └── perf.rst ├── examples ├── sum_client.py └── sum_server.py ├── mprpc ├── __init__.py ├── client.c ├── client.pyx ├── constants.py ├── exceptions.py ├── server.c └── server.pyx ├── requirements.txt ├── setup.py └── tests ├── __init__.py └── test_rpc.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.so 3 | *.egg-info 4 | *.egg 5 | .coverage 6 | .ropeproject 7 | .venv* 8 | *.sublime-* 9 | build 10 | dist 11 | docs/_build 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.6" 4 | - "2.7" 5 | - "3.3" 6 | - "3.4" 7 | - "3.5" 8 | before_install: 9 | - "sudo apt-get install python-dev" 10 | install: 11 | - pip install -r requirements.txt 12 | script: 13 | - python setup.py test 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2013 Studio Ousia 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.rst LICENSE requirements.txt 2 | include mprpc/*.c 3 | include mprpc/*.pyx 4 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | mprpc 2 | ===== 3 | 4 | ⚠️ **NOTICE:** This library is not actively maintained. Please consider using other alternatives such as `gRPC `_. 5 | 6 | mprpc is a lightweight `MessagePack RPC `_ library. It enables you to easily build a distributed server-side system by writing a small amount of code. It is built on top of `gevent `_ and `MessagePack `_. 7 | 8 | 9 | Installation 10 | ------------ 11 | 12 | To install mprpc, simply: 13 | 14 | .. code-block:: bash 15 | 16 | $ pip install mprpc 17 | 18 | Alternatively, 19 | 20 | .. code-block:: bash 21 | 22 | $ easy_install mprpc 23 | 24 | Examples 25 | -------- 26 | 27 | RPC server 28 | ^^^^^^^^^^ 29 | 30 | .. code-block:: python 31 | 32 | from gevent.server import StreamServer 33 | from mprpc import RPCServer 34 | 35 | class SumServer(RPCServer): 36 | def sum(self, x, y): 37 | return x + y 38 | 39 | server = StreamServer(('127.0.0.1', 6000), SumServer()) 40 | server.serve_forever() 41 | 42 | RPC client 43 | ^^^^^^^^^^ 44 | 45 | .. code-block:: python 46 | 47 | from mprpc import RPCClient 48 | 49 | client = RPCClient('127.0.0.1', 6000) 50 | print client.call('sum', 1, 2) 51 | 52 | 53 | RPC client with connection pooling 54 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 55 | 56 | .. code-block:: python 57 | 58 | import gsocketpool.pool 59 | from mprpc import RPCPoolClient 60 | 61 | client_pool = gsocketpool.pool.Pool(RPCPoolClient, dict(host='127.0.0.1', port=6000)) 62 | 63 | with client_pool.connection() as client: 64 | print client.call('sum', 1, 2) 65 | 66 | 67 | Performance 68 | ----------- 69 | 70 | mprpc significantly outperforms the `official MessagePack RPC `_ (**1.8x** faster), which is built using `Facebook's Tornado `_ and `MessagePack `_, and `ZeroRPC `_ (**14x** faster), which is built using `ZeroMQ `_ and `MessagePack `_. 71 | 72 | Results 73 | ^^^^^^^ 74 | 75 | .. image:: https://raw.github.com/studio-ousia/mprpc/master/docs/img/perf.png 76 | :width: 550px 77 | :height: 150px 78 | :alt: Performance Comparison 79 | 80 | mprpc 81 | ~~~~~ 82 | 83 | .. code-block:: bash 84 | 85 | % python benchmarks/benchmark.py 86 | call: 9508 qps 87 | call_using_connection_pool: 10172 qps 88 | 89 | 90 | Official MesssagePack RPC 91 | ~~~~~~~~~~~~~~~~~~~~~~~~~ 92 | 93 | .. code-block:: bash 94 | 95 | % pip install msgpack-rpc-python 96 | % python benchmarks/benchmark_msgpackrpc_official.py 97 | call: 4976 qps 98 | 99 | ZeroRPC 100 | ~~~~~~~ 101 | 102 | .. code-block:: bash 103 | 104 | % pip install zerorpc 105 | % python benchmarks/benchmark_zerorpc.py 106 | call: 655 qps 107 | 108 | 109 | Documentation 110 | ------------- 111 | 112 | Documentation is available at http://mprpc.readthedocs.org/. 113 | -------------------------------------------------------------------------------- /benchmarks/benchmark.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import print_function 3 | 4 | import time 5 | import multiprocessing 6 | 7 | import sys 8 | 9 | if sys.version_info < (3,): 10 | range = xrange 11 | else: 12 | import builtins 13 | range = builtins.range 14 | 15 | NUM_CALLS = 10000 16 | 17 | 18 | def run_sum_server(): 19 | from gevent.server import StreamServer 20 | from mprpc import RPCServer 21 | 22 | class SumServer(RPCServer): 23 | def sum(self, x, y): 24 | return x + y 25 | 26 | server = StreamServer(('127.0.0.1', 6000), SumServer()) 27 | server.serve_forever() 28 | 29 | 30 | def call(): 31 | from mprpc import RPCClient 32 | 33 | client = RPCClient('127.0.0.1', 6000) 34 | 35 | start = time.time() 36 | [client.call('sum', 1, 2) for _ in range(NUM_CALLS)] 37 | 38 | print('call: %d qps' % (NUM_CALLS / (time.time() - start))) 39 | 40 | 41 | def call_using_connection_pool(): 42 | from mprpc import RPCPoolClient 43 | 44 | import gevent.pool 45 | import gsocketpool.pool 46 | 47 | def _call(n): 48 | with client_pool.connection() as client: 49 | return client.call('sum', 1, 2) 50 | 51 | options = dict(host='127.0.0.1', port=6000) 52 | client_pool = gsocketpool.pool.Pool(RPCPoolClient, options, initial_connections=20) 53 | glet_pool = gevent.pool.Pool(20) 54 | 55 | start = time.time() 56 | 57 | [None for _ in glet_pool.imap_unordered(_call, range(NUM_CALLS))] 58 | 59 | print('call_using_connection_pool: %d qps' % (NUM_CALLS / (time.time() - start))) 60 | 61 | 62 | if __name__ == '__main__': 63 | p = multiprocessing.Process(target=run_sum_server) 64 | p.start() 65 | 66 | time.sleep(1) 67 | 68 | call() 69 | call_using_connection_pool() 70 | 71 | p.terminate() 72 | -------------------------------------------------------------------------------- /benchmarks/benchmark_msgpackrpc_official.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import print_function 3 | 4 | import time 5 | import msgpackrpc 6 | import multiprocessing 7 | 8 | import sys 9 | 10 | NUM_CALLS = 10000 11 | 12 | 13 | def run_sum_server(): 14 | class SumServer(object): 15 | def sum(self, x, y): 16 | return x + y 17 | 18 | server = msgpackrpc.Server(SumServer()) 19 | server.listen(msgpackrpc.Address("localhost", 6000)) 20 | server.start() 21 | 22 | 23 | def call(): 24 | client = msgpackrpc.Client(msgpackrpc.Address("localhost", 6000)) 25 | 26 | start = time.time() 27 | if sys.version_info < (3,): 28 | range = xrange 29 | else: 30 | import builtins 31 | range = builtins.range 32 | [client.call('sum', 1, 2) for _ in range(NUM_CALLS)] 33 | 34 | print('call: %d qps' % (NUM_CALLS / (time.time() - start))) 35 | 36 | 37 | if __name__ == '__main__': 38 | p = multiprocessing.Process(target=run_sum_server) 39 | p.start() 40 | 41 | time.sleep(1) 42 | 43 | call() 44 | 45 | p.terminate() 46 | -------------------------------------------------------------------------------- /benchmarks/benchmark_zerorpc.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import print_function 3 | import time 4 | import multiprocessing 5 | import sys 6 | 7 | NUM_CALLS = 10000 8 | 9 | 10 | def run_sum_server(): 11 | import zerorpc 12 | 13 | class SumServer(object): 14 | def sum(self, x, y): 15 | return x + y 16 | 17 | server = zerorpc.Server(SumServer()) 18 | server.bind("tcp://127.0.0.1:6000") 19 | server.run() 20 | 21 | 22 | def call(): 23 | import zerorpc 24 | 25 | client = zerorpc.Client() 26 | client.connect("tcp://127.0.0.1:6000") 27 | 28 | start = time.time() 29 | 30 | if sys.version_info < (3,): 31 | range = xrange 32 | else: 33 | import builtins 34 | range = builtins.range 35 | [client.sum(1, 2) for _ in range(NUM_CALLS)] 36 | 37 | print('call: %d qps' % (NUM_CALLS / (time.time() - start))) 38 | 39 | 40 | if __name__ == '__main__': 41 | p = multiprocessing.Process(target=run_sum_server) 42 | p.start() 43 | 44 | time.sleep(1) 45 | 46 | call() 47 | 48 | p.terminate() 49 | -------------------------------------------------------------------------------- /cythonize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cython mprpc/*.pyx 4 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/mprpc.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/mprpc.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/mprpc" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/mprpc" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/api.rst: -------------------------------------------------------------------------------- 1 | API Reference 2 | ============= 3 | 4 | .. autoclass:: mprpc.RPCClient 5 | :inherited-members: 6 | 7 | .. autoclass:: mprpc.RPCServer 8 | :inherited-members: 9 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # mprpc documentation build configuration file, created by 4 | # sphinx-quickstart on Mon Oct 14 17:20:59 2013. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys 16 | import os 17 | import re 18 | 19 | # If extensions (or modules to document with autodoc) are in another directory, 20 | # add these directories to sys.path here. If the directory is relative to the 21 | # documentation root, use os.path.abspath to make it absolute, like shown here. 22 | #sys.path.insert(0, os.path.abspath('.')) 23 | 24 | # -- General configuration ------------------------------------------------ 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | #needs_sphinx = '1.0' 28 | 29 | # Add any Sphinx extension module names here, as strings. They can be 30 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 31 | # ones. 32 | extensions = [ 33 | 'sphinx.ext.autodoc', 34 | ] 35 | 36 | # Add any paths that contain templates here, relative to this directory. 37 | templates_path = ['_templates'] 38 | 39 | # The suffix of source filenames. 40 | source_suffix = '.rst' 41 | 42 | # The encoding of source files. 43 | #source_encoding = 'utf-8-sig' 44 | 45 | # The master toctree document. 46 | master_doc = 'index' 47 | 48 | # General information about the project. 49 | project = u'mprpc' 50 | copyright = u'2013, Studio Ousia' 51 | 52 | # The version info for the project you're documenting, acts as replacement for 53 | # |version| and |release|, also used in various other places throughout the 54 | # built documents. 55 | # 56 | release = re.search("version='([^']+)'", 57 | open(os.path.join(os.path.dirname(__file__), os.pardir, 58 | 'setup.py')).read().strip() 59 | ).group(1) 60 | 61 | # The short X.Y version. 62 | version = '.'.join(release.split('.')[:2]) 63 | 64 | # The language for content autogenerated by Sphinx. Refer to documentation 65 | # for a list of supported languages. 66 | #language = None 67 | 68 | # There are two options for replacing |today|: either, you set today to some 69 | # non-false value, then it is used: 70 | #today = '' 71 | # Else, today_fmt is used as the format for a strftime call. 72 | #today_fmt = '%B %d, %Y' 73 | 74 | # List of patterns, relative to source directory, that match files and 75 | # directories to ignore when looking for source files. 76 | exclude_patterns = ['_build'] 77 | 78 | # The reST default role (used for this markup: `text`) to use for all 79 | # documents. 80 | #default_role = None 81 | 82 | # If true, '()' will be appended to :func: etc. cross-reference text. 83 | #add_function_parentheses = True 84 | 85 | # If true, the current module name will be prepended to all description 86 | # unit titles (such as .. function::). 87 | #add_module_names = True 88 | 89 | # If true, sectionauthor and moduleauthor directives will be shown in the 90 | # output. They are ignored by default. 91 | #show_authors = False 92 | 93 | # The name of the Pygments (syntax highlighting) style to use. 94 | pygments_style = 'sphinx' 95 | 96 | # A list of ignored prefixes for module index sorting. 97 | #modindex_common_prefix = [] 98 | 99 | # If true, keep warnings as "system message" paragraphs in the built documents. 100 | #keep_warnings = False 101 | 102 | 103 | # -- Options for HTML output ---------------------------------------------- 104 | 105 | # The theme to use for HTML and HTML Help pages. See the documentation for 106 | # a list of builtin themes. 107 | html_theme = 'default' 108 | 109 | # Theme options are theme-specific and customize the look and feel of a theme 110 | # further. For a list of options available for each theme, see the 111 | # documentation. 112 | #html_theme_options = {} 113 | 114 | # Add any paths that contain custom themes here, relative to this directory. 115 | #html_theme_path = [] 116 | 117 | # The name for this set of Sphinx documents. If None, it defaults to 118 | # " v documentation". 119 | #html_title = None 120 | 121 | # A shorter title for the navigation bar. Default is the same as html_title. 122 | #html_short_title = None 123 | 124 | # The name of an image file (relative to this directory) to place at the top 125 | # of the sidebar. 126 | #html_logo = None 127 | 128 | # The name of an image file (within the static path) to use as favicon of the 129 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 130 | # pixels large. 131 | #html_favicon = None 132 | 133 | # Add any paths that contain custom static files (such as style sheets) here, 134 | # relative to this directory. They are copied after the builtin static files, 135 | # so a file named "default.css" will overwrite the builtin "default.css". 136 | html_static_path = ['_static'] 137 | 138 | # Add any extra paths that contain custom files (such as robots.txt or 139 | # .htaccess) here, relative to this directory. These files are copied 140 | # directly to the root of the documentation. 141 | #html_extra_path = [] 142 | 143 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 144 | # using the given strftime format. 145 | #html_last_updated_fmt = '%b %d, %Y' 146 | 147 | # If true, SmartyPants will be used to convert quotes and dashes to 148 | # typographically correct entities. 149 | #html_use_smartypants = True 150 | 151 | # Custom sidebar templates, maps document names to template names. 152 | #html_sidebars = {} 153 | 154 | # Additional templates that should be rendered to pages, maps page names to 155 | # template names. 156 | #html_additional_pages = {} 157 | 158 | # If false, no module index is generated. 159 | #html_domain_indices = True 160 | 161 | # If false, no index is generated. 162 | #html_use_index = True 163 | 164 | # If true, the index is split into individual pages for each letter. 165 | #html_split_index = False 166 | 167 | # If true, links to the reST sources are added to the pages. 168 | #html_show_sourcelink = True 169 | 170 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 171 | #html_show_sphinx = True 172 | 173 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 174 | #html_show_copyright = True 175 | 176 | # If true, an OpenSearch description file will be output, and all pages will 177 | # contain a tag referring to it. The value of this option must be the 178 | # base URL from which the finished HTML is served. 179 | #html_use_opensearch = '' 180 | 181 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 182 | #html_file_suffix = None 183 | 184 | # Output file base name for HTML help builder. 185 | htmlhelp_basename = 'mprpcdoc' 186 | 187 | 188 | # -- Options for LaTeX output --------------------------------------------- 189 | 190 | latex_elements = { 191 | # The paper size ('letterpaper' or 'a4paper'). 192 | #'papersize': 'letterpaper', 193 | 194 | # The font size ('10pt', '11pt' or '12pt'). 195 | #'pointsize': '10pt', 196 | 197 | # Additional stuff for the LaTeX preamble. 198 | #'preamble': '', 199 | } 200 | 201 | # Grouping the document tree into LaTeX files. List of tuples 202 | # (source start file, target name, title, 203 | # author, documentclass [howto, manual, or own class]). 204 | latex_documents = [ 205 | ('index', 'mprpc.tex', u'mprpc Documentation', 206 | u'Studio Ousia', 'manual'), 207 | ] 208 | 209 | # The name of an image file (relative to this directory) to place at the top of 210 | # the title page. 211 | #latex_logo = None 212 | 213 | # For "manual" documents, if this is true, then toplevel headings are parts, 214 | # not chapters. 215 | #latex_use_parts = False 216 | 217 | # If true, show page references after internal links. 218 | #latex_show_pagerefs = False 219 | 220 | # If true, show URL addresses after external links. 221 | #latex_show_urls = False 222 | 223 | # Documents to append as an appendix to all manuals. 224 | #latex_appendices = [] 225 | 226 | # If false, no module index is generated. 227 | #latex_domain_indices = True 228 | 229 | 230 | # -- Options for manual page output --------------------------------------- 231 | 232 | # One entry per manual page. List of tuples 233 | # (source start file, name, description, authors, manual section). 234 | man_pages = [ 235 | ('index', 'mprpc', u'mprpc Documentation', 236 | [u'Studio Ousia'], 1) 237 | ] 238 | 239 | # If true, show URL addresses after external links. 240 | #man_show_urls = False 241 | 242 | 243 | # -- Options for Texinfo output ------------------------------------------- 244 | 245 | # Grouping the document tree into Texinfo files. List of tuples 246 | # (source start file, target name, title, author, 247 | # dir menu entry, description, category) 248 | texinfo_documents = [ 249 | ('index', 'mprpc', u'mprpc Documentation', 250 | u'Studio Ousia', 'mprpc', 'One line description of project.', 251 | 'Miscellaneous'), 252 | ] 253 | 254 | # Documents to append as an appendix to all manuals. 255 | #texinfo_appendices = [] 256 | 257 | # If false, no module index is generated. 258 | #texinfo_domain_indices = True 259 | 260 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 261 | #texinfo_show_urls = 'footnote' 262 | 263 | # If true, do not generate a @detailmenu in the "Top" node's menu. 264 | #texinfo_no_detailmenu = False 265 | -------------------------------------------------------------------------------- /docs/img/perf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studio-ousia/mprpc/6076f68a16f78e0010307344afa253e0956f2a9d/docs/img/perf.png -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. mprpc documentation master file, created by 2 | sphinx-quickstart on Mon Oct 14 17:20:59 2013. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to mprpc's documentation! 7 | ================================= 8 | 9 | mprpc is a lightweight `MessagePack RPC `_ library. It enables you to easily build a distributed server-side system by writing a small amount of code. It is built on top of `gevent `_ and `MessagePack `_. 10 | 11 | Contents: 12 | 13 | .. toctree:: 14 | :maxdepth: 2 15 | 16 | intro 17 | perf 18 | api 19 | 20 | 21 | 22 | Indices and tables 23 | ================== 24 | 25 | * :ref:`genindex` 26 | * :ref:`modindex` 27 | * :ref:`search` 28 | 29 | -------------------------------------------------------------------------------- /docs/intro.rst: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | Installation 5 | ------------ 6 | 7 | To install mprpc, simply: 8 | 9 | .. code-block:: bash 10 | 11 | $ pip install mprpc 12 | 13 | Alternatively, 14 | 15 | .. code-block:: bash 16 | 17 | $ easy_install mprpc 18 | 19 | Examples 20 | -------- 21 | 22 | RPC server 23 | ^^^^^^^^^^ 24 | 25 | .. code-block:: python 26 | 27 | from gevent.server import StreamServer 28 | from mprpc import RPCServer 29 | 30 | class SumServer(RPCServer): 31 | def sum(self, x, y): 32 | return x + y 33 | 34 | server = StreamServer(('127.0.0.1', 6000), SumServer()) 35 | server.serve_forever() 36 | 37 | 38 | RPC client 39 | ^^^^^^^^^^ 40 | 41 | .. code-block:: python 42 | 43 | from mprpc import RPCClient 44 | 45 | client = RPCClient('127.0.0.1', 6000) 46 | print client.call('sum', 1, 2) 47 | 48 | 49 | RPC client with connection pooling using `gsocketpool `_ 50 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 51 | 52 | .. code-block:: python 53 | 54 | import gsocketpool.pool 55 | from mprpc import RPCPoolClient 56 | 57 | client_pool = gsocketpool.pool.Pool(RPCPoolClient, dict(host='127.0.0.1', port=6000)) 58 | 59 | with client_pool.connection() as client: 60 | print client.call('sum', 1, 2) 61 | -------------------------------------------------------------------------------- /docs/perf.rst: -------------------------------------------------------------------------------- 1 | Performance 2 | =========== 3 | 4 | mprpc significantly outperforms the `official MessagePack RPC `_ (**1.8x** faster), which is built using `Facebook's Tornado `_ and `MessagePack `_, and `ZeroRPC `_ (**14x** faster), which is built using `ZeroMQ `_ and `MessagePack `_. 5 | 6 | Results 7 | ------- 8 | 9 | .. image:: img/perf.png 10 | :width: 500px 11 | :height: 150px 12 | :alt: Performance Comparison 13 | 14 | mprpc 15 | ^^^^^ 16 | 17 | .. code-block:: bash 18 | 19 | % python benchmarks/benchmark.py 20 | call: 9508 qps 21 | call_using_connection_pool: 10172 qps 22 | 23 | 24 | Official MesssagePack RPC 25 | ^^^^^^^^^^^^^^^^^^^^^^^^^ 26 | 27 | .. code-block:: bash 28 | 29 | % pip install msgpack-rpc-python 30 | % python benchmarks/benchmark_msgpackrpc_official.py 31 | call: 4976 qps 32 | 33 | ZeroRPC 34 | ^^^^^^^ 35 | 36 | .. code-block:: bash 37 | 38 | % pip install zerorpc 39 | % python benchmarks/benchmark_zerorpc.py 40 | call: 655 qps 41 | 42 | 43 | Environment 44 | ----------- 45 | 46 | - OS: Mac OS X 10.8.5 47 | - CPU: Intel Core i7 2GHz 48 | - Memory: 8GB 49 | - Python: 2.7.3 50 | -------------------------------------------------------------------------------- /examples/sum_client.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import gsocketpool.pool 4 | import gevent.pool 5 | 6 | from mprpc import RPCClient, RPCPoolClient 7 | 8 | 9 | def call(): 10 | client = RPCClient('127.0.0.1', 6000) 11 | 12 | print client.call('sum', 1, 2) 13 | 14 | 15 | def call_using_pool(): 16 | options = dict(host='127.0.0.1', port=6000) 17 | client_pool = gsocketpool.pool.Pool(RPCPoolClient, options) 18 | 19 | def _call(n): 20 | with client_pool.connection() as client: 21 | return client.call('sum', 1, 2) 22 | 23 | glet_pool = gevent.pool.Pool(10) 24 | print [result for result in glet_pool.imap_unordered(_call, xrange(10))] 25 | 26 | 27 | call() 28 | call_using_pool() 29 | -------------------------------------------------------------------------------- /examples/sum_server.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from gevent.server import StreamServer 4 | 5 | from mprpc import RPCServer 6 | 7 | 8 | class SumServer(RPCServer): 9 | def sum(self, x, y): 10 | return x + y 11 | 12 | 13 | server = StreamServer(('127.0.0.1', 6000), SumServer()) 14 | server.serve_forever() 15 | -------------------------------------------------------------------------------- /mprpc/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from __future__ import absolute_import 4 | 5 | import logging 6 | try: # Python 2.7+ 7 | from logging import NullHandler 8 | except ImportError: 9 | class NullHandler(logging.Handler): 10 | def emit(self, record): 11 | pass 12 | 13 | logger = logging.getLogger(__name__) 14 | logger.addHandler(NullHandler()) 15 | 16 | from mprpc.client import RPCClient, RPCPoolClient 17 | from mprpc.server import RPCServer 18 | -------------------------------------------------------------------------------- /mprpc/client.pyx: -------------------------------------------------------------------------------- 1 | # cython: profile=False 2 | # -*- coding: utf-8 -*- 3 | 4 | import msgpack 5 | import time 6 | from gevent import socket 7 | from gsocketpool.connection import Connection 8 | 9 | from mprpc.exceptions import RPCProtocolError, RPCError 10 | from mprpc.constants import MSGPACKRPC_REQUEST, MSGPACKRPC_RESPONSE, SOCKET_RECV_SIZE 11 | 12 | from mprpc import logger 13 | 14 | 15 | cdef class RPCClient: 16 | """RPC client. 17 | 18 | Usage: 19 | >>> from mprpc import RPCClient 20 | >>> client = RPCClient('127.0.0.1', 6000) 21 | >>> print client.call('sum', 1, 2) 22 | 3 23 | 24 | :param str host: Hostname. 25 | :param int port: Port number. 26 | :param int timeout: (optional) Socket timeout. 27 | :param bool lazy: (optional) If set to True, the socket connection is not 28 | established until you specifically call open() 29 | :param str pack_encoding: (optional) Character encoding used to pack data 30 | using Messagepack. 31 | :param str unpack_encoding: (optional) Character encoding used to unpack 32 | data using Messagepack. 33 | :param dict pack_params: (optional) Parameters to pass to Messagepack Packer 34 | :param dict unpack_params: (optional) Parameters to pass to Messagepack 35 | :param tcp_no_delay (optional) If set to True, use TCP_NODELAY. 36 | :param keep_alive (optional) If set to True, use socket keep alive. 37 | Unpacker 38 | """ 39 | 40 | cdef str _host 41 | cdef int _port 42 | cdef int _msg_id 43 | cdef _timeout 44 | cdef _socket 45 | cdef _packer 46 | cdef _pack_params 47 | cdef _unpack_encoding 48 | cdef _unpack_params 49 | cdef _tcp_no_delay 50 | cdef _keep_alive 51 | 52 | def __init__(self, host, port, timeout=None, lazy=False, 53 | pack_encoding='utf-8', unpack_encoding='utf-8', 54 | pack_params=None, unpack_params=None, 55 | tcp_no_delay=False, keep_alive=False): 56 | self._host = host 57 | self._port = port 58 | self._timeout = timeout 59 | 60 | self._msg_id = 0 61 | self._socket = None 62 | self._tcp_no_delay = tcp_no_delay 63 | self._keep_alive = keep_alive 64 | self._pack_params = pack_params or dict(use_bin_type=True) 65 | self._unpack_encoding = unpack_encoding 66 | self._unpack_params = unpack_params or dict(use_list=False) 67 | 68 | self._packer = msgpack.Packer(encoding=pack_encoding, **self._pack_params) 69 | 70 | if not lazy: 71 | self.open() 72 | 73 | def getpeername(self): 74 | """Return the address of the remote endpoint.""" 75 | return self._host, self._port 76 | 77 | def open(self): 78 | """Opens a connection.""" 79 | 80 | assert self._socket is None, 'The connection has already been established' 81 | 82 | logger.debug('openning a msgpackrpc connection') 83 | 84 | if self._timeout: 85 | self._socket = socket.create_connection((self._host, self._port), 86 | self._timeout) 87 | else: 88 | # use the default timeout value 89 | self._socket = socket.create_connection((self._host, self._port)) 90 | 91 | # set TCP NODELAY 92 | if self._tcp_no_delay: 93 | self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) 94 | 95 | # set KEEP_ALIVE 96 | if self._keep_alive: 97 | self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) 98 | 99 | def close(self): 100 | """Closes the connection.""" 101 | 102 | assert self._socket is not None, 'Attempt to close an unopened socket' 103 | 104 | logger.debug('Closing a msgpackrpc connection') 105 | try: 106 | self._socket.close() 107 | except: 108 | logger.exception('An error has occurred while closing the socket') 109 | 110 | self._socket = None 111 | 112 | def is_connected(self): 113 | """Returns whether the connection has already been established. 114 | 115 | :rtype: bool 116 | """ 117 | 118 | if self._socket: 119 | return True 120 | else: 121 | return False 122 | 123 | def call(self, str method, *args): 124 | """Calls a RPC method. 125 | 126 | :param str method: Method name. 127 | :param args: Method arguments. 128 | """ 129 | 130 | cdef bytes req = self._create_request(method, args) 131 | 132 | cdef bytes data 133 | self._socket.sendall(req) 134 | 135 | unpacker = msgpack.Unpacker(encoding=self._unpack_encoding, 136 | **self._unpack_params) 137 | while True: 138 | data = self._socket.recv(SOCKET_RECV_SIZE) 139 | if not data: 140 | raise IOError('Connection closed') 141 | unpacker.feed(data) 142 | try: 143 | response = next(unpacker) 144 | break 145 | except StopIteration: 146 | continue 147 | 148 | if type(response) not in (tuple, list): 149 | logger.debug('Protocol error, received unexpected data: {}'.format(data)) 150 | raise RPCProtocolError('Invalid protocol') 151 | 152 | return self._parse_response(response) 153 | 154 | cdef bytes _create_request(self, method, args): 155 | self._msg_id += 1 156 | 157 | cdef tuple req 158 | req = (MSGPACKRPC_REQUEST, self._msg_id, method, args) 159 | 160 | return self._packer.pack(req) 161 | 162 | cdef _parse_response(self, response): 163 | if (len(response) != 4 or response[0] != MSGPACKRPC_RESPONSE): 164 | raise RPCProtocolError('Invalid protocol') 165 | 166 | cdef int msg_id 167 | (_, msg_id, error, result) = response 168 | 169 | if msg_id != self._msg_id: 170 | raise RPCError('Invalid Message ID') 171 | 172 | if error: 173 | raise RPCError(str(error)) 174 | 175 | return result 176 | 177 | 178 | class RPCPoolClient(RPCClient, Connection): 179 | """Wrapper class of :class:`RPCClient ` for `gsocketpool `_. 180 | 181 | Usage: 182 | >>> import gsocketpool.pool 183 | >>> from mprpc import RPCPoolClient 184 | >>> client_pool = gsocketpool.pool.Pool(RPCPoolClient, dict(host='127.0.0.1', port=6000)) 185 | >>> with client_pool.connection() as client: 186 | ... print client.call('sum', 1, 2) 187 | ... 188 | 3 189 | 190 | :param str host: Hostname. 191 | :param int port: Port number. 192 | :param int timeout: (optional) Socket timeout. 193 | :param int lifetime: (optional) Connection lifetime in seconds. Only valid 194 | when used with `gsocketpool.pool.Pool `_. 195 | :param str pack_encoding: (optional) Character encoding used to pack data 196 | using Messagepack. 197 | :param str unpack_encoding: (optional) Character encoding used to unpack 198 | data using Messagepack. 199 | :param dict pack_params: (optional) Parameters to pass to Messagepack Packer 200 | :param dict unpack_params: (optional) Parameters to pass to Messagepack 201 | :param tcp_no_delay (optional) If set to True, use TCP_NODELAY. 202 | :param keep_alive (optional) If set to True, use socket keep alive. 203 | Unpacker 204 | """ 205 | 206 | def __init__(self, host, port, timeout=None, lifetime=None, 207 | pack_encoding='utf-8', unpack_encoding='utf-8', 208 | pack_params=dict(), unpack_params=dict(use_list=False), 209 | tcp_no_delay=False, keep_alive=False): 210 | 211 | if lifetime: 212 | assert lifetime > 0, 'Lifetime must be a positive value' 213 | self._lifetime = time.time() + lifetime 214 | else: 215 | self._lifetime = None 216 | 217 | RPCClient.__init__( 218 | self, host, port, timeout=timeout, lazy=True, 219 | pack_encoding=pack_encoding, unpack_encoding=unpack_encoding, 220 | pack_params=pack_params, unpack_params=unpack_params, 221 | tcp_no_delay=tcp_no_delay, keep_alive=keep_alive) 222 | 223 | def is_expired(self): 224 | """Returns whether the connection has been expired. 225 | 226 | :rtype: bool 227 | """ 228 | 229 | if not self._lifetime or time.time() > self._lifetime: 230 | return True 231 | 232 | else: 233 | return False 234 | 235 | def call(self, str method, *args): 236 | """Calls a RPC method. 237 | 238 | :param str method: Method name. 239 | :param args: Method arguments. 240 | """ 241 | 242 | try: 243 | return RPCClient.call(self, method, *args) 244 | 245 | except socket.timeout: 246 | self.reconnect() 247 | raise 248 | 249 | except IOError: 250 | self.reconnect() 251 | raise 252 | -------------------------------------------------------------------------------- /mprpc/constants.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | MSGPACKRPC_REQUEST = 0 4 | MSGPACKRPC_RESPONSE = 1 5 | SOCKET_RECV_SIZE = 1024 ** 2 6 | -------------------------------------------------------------------------------- /mprpc/exceptions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | class RPCProtocolError(Exception): 5 | pass 6 | 7 | 8 | class MethodNotFoundError(Exception): 9 | pass 10 | 11 | 12 | class RPCError(Exception): 13 | pass 14 | -------------------------------------------------------------------------------- /mprpc/server.pyx: -------------------------------------------------------------------------------- 1 | # cython: profile=False 2 | # -*- coding: utf-8 -*- 3 | 4 | import gevent.socket 5 | import msgpack 6 | 7 | from constants import MSGPACKRPC_REQUEST, MSGPACKRPC_RESPONSE, SOCKET_RECV_SIZE 8 | from exceptions import MethodNotFoundError, RPCProtocolError 9 | from gevent.local import local 10 | 11 | 12 | cdef class RPCServer: 13 | """RPC server. 14 | 15 | This class is assumed to be used with gevent StreamServer. 16 | 17 | :param str pack_encoding: (optional) Character encoding used to pack data 18 | using Messagepack. 19 | :param str unpack_encoding: (optional) Character encoding used to unpack 20 | data using Messagepack 21 | :param dict pack_params: (optional) Parameters to pass to Messagepack Packer 22 | :param dict unpack_params: (optional) Parameters to pass to Messagepack 23 | Unpacker 24 | 25 | Usage: 26 | >>> from gevent.server import StreamServer 27 | >>> import mprpc 28 | >>> 29 | >>> class SumServer(mprpc.RPCServer): 30 | ... def sum(self, x, y): 31 | ... return x + y 32 | ... 33 | >>> 34 | >>> server = StreamServer(('127.0.0.1', 6000), SumServer()) 35 | >>> server.serve_forever() 36 | """ 37 | 38 | cdef _packer 39 | cdef _unpack_encoding 40 | cdef _unpack_params 41 | cdef _tcp_no_delay 42 | cdef _methods 43 | cdef _address 44 | 45 | def __init__(self, *args, **kwargs): 46 | pack_encoding = kwargs.pop('pack_encoding', 'utf-8') 47 | pack_params = kwargs.pop('pack_params', dict(use_bin_type=True)) 48 | 49 | self._unpack_encoding = kwargs.pop('unpack_encoding', 'utf-8') 50 | self._unpack_params = kwargs.pop('unpack_params', dict(use_list=False)) 51 | 52 | self._tcp_no_delay = kwargs.pop('tcp_no_delay', False) 53 | self._methods = {} 54 | 55 | self._packer = msgpack.Packer(encoding=pack_encoding, **pack_params) 56 | 57 | self._address = local() 58 | self._address.client_host = None 59 | self._address.client_port = None 60 | 61 | if args and isinstance(args[0], gevent.socket.socket): 62 | self._run(_RPCConnection(args[0])) 63 | 64 | def __call__(self, sock, address): 65 | if self._tcp_no_delay: 66 | sock.setsockopt(gevent.socket.IPPROTO_TCP, gevent.socket.TCP_NODELAY, 1) 67 | 68 | self._address.client_host = address[0] 69 | self._address.client_port = address[1] 70 | 71 | self._run(_RPCConnection(sock)) 72 | 73 | property client_host: 74 | def __get__(self): 75 | return self._address.client_host 76 | 77 | property client_port: 78 | def __get__(self): 79 | return self._address.client_port 80 | 81 | def _run(self, _RPCConnection conn): 82 | cdef bytes data 83 | cdef int msg_id 84 | 85 | unpacker = msgpack.Unpacker(encoding=self._unpack_encoding, 86 | **self._unpack_params) 87 | while True: 88 | data = conn.recv(SOCKET_RECV_SIZE) 89 | if not data: 90 | break 91 | 92 | unpacker.feed(data) 93 | try: 94 | req = next(unpacker) 95 | except StopIteration: 96 | continue 97 | 98 | if type(req) not in (tuple, list): 99 | self._send_error("Invalid protocol", -1, conn) 100 | # reset unpacker as it might have garbage data 101 | unpacker = msgpack.Unpacker(encoding=self._unpack_encoding, 102 | **self._unpack_params) 103 | continue 104 | 105 | (msg_id, method, args) = self._parse_request(req) 106 | 107 | try: 108 | ret = method(*args) 109 | 110 | except Exception, e: 111 | self._send_error(str(e), msg_id, conn) 112 | 113 | else: 114 | self._send_result(ret, msg_id, conn) 115 | 116 | cdef tuple _parse_request(self, req): 117 | if (len(req) != 4 or req[0] != MSGPACKRPC_REQUEST): 118 | raise RPCProtocolError('Invalid protocol') 119 | 120 | cdef int msg_id 121 | 122 | (_, msg_id, method_name, args) = req 123 | 124 | method = self._methods.get(method_name, None) 125 | 126 | if method is None: 127 | if method_name.startswith('_'): 128 | raise MethodNotFoundError('Method not found: %s', method_name) 129 | 130 | if not hasattr(self, method_name): 131 | raise MethodNotFoundError('Method not found: %s', method_name) 132 | 133 | method = getattr(self, method_name) 134 | if not hasattr(method, '__call__'): 135 | raise MethodNotFoundError('Method is not callable: %s', method_name) 136 | 137 | self._methods[method_name] = method 138 | 139 | return (msg_id, method, args) 140 | 141 | cdef _send_result(self, object result, int msg_id, _RPCConnection conn): 142 | msg = (MSGPACKRPC_RESPONSE, msg_id, None, result) 143 | conn.send(self._packer.pack(msg)) 144 | 145 | cdef _send_error(self, str error, int msg_id, _RPCConnection conn): 146 | msg = (MSGPACKRPC_RESPONSE, msg_id, error, None) 147 | conn.send(self._packer.pack(msg)) 148 | 149 | 150 | cdef class _RPCConnection: 151 | cdef _socket 152 | 153 | def __init__(self, socket): 154 | self._socket = socket 155 | 156 | cdef recv(self, int buf_size): 157 | return self._socket.recv(buf_size) 158 | 159 | cdef send(self, bytes msg): 160 | self._socket.sendall(msg) 161 | 162 | def __del__(self): 163 | try: 164 | self._socket.close() 165 | except: 166 | pass 167 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.21.2 2 | gevent==1.1b5 3 | greenlet==0.4.9 4 | gsocketpool==0.1.5 5 | mock==1.0.1 6 | msgpack-python==0.4.2 7 | nose==1.3.7 8 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from setuptools import setup, Extension 4 | 5 | setup( 6 | name='mprpc', 7 | version='0.1.17', 8 | description='A fast MessagePack RPC library', 9 | long_description=open('README.rst').read(), 10 | author='Studio Ousia', 11 | author_email='ikuya@ousia.jp', 12 | url='http://github.com/studio-ousia/mprpc', 13 | packages=['mprpc'], 14 | ext_modules=[ 15 | Extension('mprpc.client', ['mprpc/client.c']), 16 | Extension('mprpc.server', ['mprpc/server.c']) 17 | ], 18 | license=open('LICENSE').read(), 19 | include_package_data=True, 20 | keywords=['rpc', 'msgpack', 'messagepack', 'msgpackrpc', 'messagepackrpc', 21 | 'messagepack rpc', 'gevent'], 22 | classifiers=[ 23 | 'Development Status :: 4 - Beta', 24 | 'Intended Audience :: Developers', 25 | 'Natural Language :: English', 26 | 'License :: OSI Approved :: Apache Software License', 27 | 'Programming Language :: Python', 28 | 'Programming Language :: Python :: 2.6', 29 | 'Programming Language :: Python :: 2.7', 30 | 'Programming Language :: Python :: 3.3', 31 | 'Programming Language :: Python :: 3.4', 32 | 'Programming Language :: Python :: 3.5', 33 | ], 34 | install_requires=[ 35 | 'gsocketpool', 36 | 'gevent', 37 | 'msgpack-python', 38 | ], 39 | tests_require=[ 40 | 'nose', 41 | 'mock', 42 | ], 43 | test_suite = 'nose.collector' 44 | ) 45 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studio-ousia/mprpc/6076f68a16f78e0010307344afa253e0956f2a9d/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_rpc.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import gevent 4 | from gevent import socket 5 | from gevent.server import StreamServer 6 | 7 | from nose.tools import * 8 | from mock import Mock, patch 9 | 10 | from mprpc.client import RPCClient 11 | from mprpc.server import RPCServer 12 | from mprpc.exceptions import RPCError 13 | 14 | HOST = 'localhost' 15 | PORT = 6000 16 | 17 | 18 | class TestRPC(object): 19 | def setUp(self): 20 | class TestServer(RPCServer): 21 | def echo(self, msg): 22 | return msg 23 | 24 | def echo_delayed(self, msg, delay): 25 | gevent.sleep(delay) 26 | return msg 27 | 28 | def raise_error(self): 29 | raise Exception('error msg') 30 | 31 | self._server = StreamServer((HOST, PORT), TestServer()) 32 | self._glet = gevent.spawn(self._server.serve_forever) 33 | 34 | def tearDown(self): 35 | self._glet.kill() 36 | 37 | @patch('mprpc.client.socket') 38 | def test_open_and_close(self, mock_socket): 39 | mock_socket_ins = Mock() 40 | mock_socket.create_connection.return_value = mock_socket_ins 41 | 42 | client = RPCClient(HOST, PORT) 43 | 44 | ok_(client.is_connected()) 45 | 46 | mock_socket.create_connection.assert_called_once_with((HOST, PORT)) 47 | 48 | client.close() 49 | 50 | ok_(mock_socket_ins.close.called) 51 | ok_(not client.is_connected()) 52 | 53 | @patch('mprpc.client.socket') 54 | def test_open_with_timeout(self, mock_socket): 55 | mock_socket_ins = Mock() 56 | mock_socket.create_connection.return_value = mock_socket_ins 57 | 58 | client = RPCClient(HOST, PORT, timeout=5.0) 59 | 60 | mock_socket.create_connection.assert_called_once_with((HOST, PORT), 5.0) 61 | ok_(client.is_connected()) 62 | 63 | def test_call(self): 64 | client = RPCClient(HOST, PORT) 65 | 66 | ret = client.call('echo', 'message') 67 | eq_('message', ret) 68 | 69 | ret = client.call('echo', 'message' * 100) 70 | eq_('message' * 100, ret) 71 | 72 | @raises(RPCError) 73 | def test_call_server_side_exception(self): 74 | client = RPCClient(HOST, PORT) 75 | 76 | try: 77 | ret = client.call('raise_error') 78 | except RPCError as e: 79 | eq_('error msg', str(e)) 80 | raise 81 | 82 | eq_('message', ret) 83 | 84 | @raises(socket.timeout) 85 | def test_call_socket_timeout(self): 86 | client = RPCClient(HOST, PORT, timeout=0.1) 87 | 88 | client.call('echo_delayed', 'message', 1) 89 | --------------------------------------------------------------------------------