{ "cells": [ { "cell_type": "markdown", "source": [ "RPC\n", "===\n", "\n", "This example walks you through a Remote Procedure Call (RPC) example" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import kiwipy\n", "comm = kiwipy.connect('amqp://127.0.0.1')" ] }, { "cell_type": "markdown", "source": [ "Let's define a couple of 'remote' procedures that can be we then expose to our communicator:" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 2, "outputs": [ { "data": { "text/plain": "'fac'" }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fib(comm, num):\n", " if num == 0:\n", " return 0\n", " if num == 1:\n", " return 1\n", " return fib(comm, num - 1) + fib(comm, num - 2)\n", "\n", "\n", "def fac(comm, num):\n", " result = 1\n", " if num > 1:\n", " result = num * fac(comm, num - 1)\n", " return result\n", "\n", "\n", "comm.add_rpc_subscriber(fib, 'fib')\n", "comm.add_rpc_subscriber(fac, 'fac')" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "Now, let's call these using their identifier" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "source": [ "fib_future = comm.rpc_send('fib', 30)\n", "fib_future.result()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "execution_count": 3, "outputs": [ { "data": { "text/plain": "832040" }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ] }, { "cell_type": "markdown", "source": [ "*Cool, let's have a look at fac then!*\n", "\n", "Ok, if you insist." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "source": [ "comm.rpc_send('fac', 3).result()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "execution_count": 4, "outputs": [ { "data": { "text/plain": "6" }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ] }, { "cell_type": "markdown", "source": [ "*Nice. So, what's the score, this looks a lot like [tasks](tasks.ipynb)?*\n", "\n", "Yup, it's pretty similar but the difference is in the use case. RPC calls have one unique recipient, for example\n", "imagine calling a class method remotely using an RPC to affect it's state. Tasks on the other hand can have zero or\n", "more identical workers that process the incoming tasks.\n", "\n", "Got it? Good." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "source": [ "comm.close()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "execution_count": 5, "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }