{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Broadcast Example\n", "=================\n", "\n", "Let's start off in the usual way and create a RabbitMQ communicator and subscribe to receive broadcasts." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import kiwipy\n", "comm = kiwipy.connect('amqp://127.0.0.1')\n", "\n", "def subscriber(_comm, body, sender, subject, _corr_id):\n", " print(\"Broadcast received:\")\n", " print(\"sender:\\t{}\\nsubject:{}\\nbody:\\t{}\\n\".format(\n", " sender, subject, body))\n", "\n", "\n", "sub_id = comm.add_broadcast_subscriber(subscriber)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Now, let's broadcast!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Broadcast received:\n", "sender:\tThe Professor\n", "subject:Good news\n", "body:\tYou'll be making a delivery to Ebola 9 tomorrow\n", "\n" ] }, { "data": { "text/plain": "True" }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "comm.broadcast_send(\n", " body=\"You'll be making a delivery to Ebola 9 tomorrow\",\n", " sender=\"The Professor\",\n", " subject=\"Good news\")" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "*True?! That doesn't sound like good news.*\n", "\n", "No, no, that's just the return value from `broadcast_send` to tell you the message was indeed sent.\n", "\n", "*Ok, but it's not exactly the kind of message I wanted to receive.*\n", "\n", "Sorry about that, here's how to filter so you only get messages that you're interested in.\n", "\n", "Broadcast Filters\n", "-----------------" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Broadcast received:\n", "sender:\tBob Jones\n", "subject:sell.caravan\n", "body:\tTip-top caravan for sale\n", "\n" ] }, { "data": { "text/plain": "True" }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Remove the current subscriber\n", "comm.remove_broadcast_subscriber(sub_id)\n", "# Add a filtered one\n", "filtered = kiwipy.BroadcastFilter(subscriber)\n", "filtered.add_subject_filter(\"sell.*\")\n", "comm.add_broadcast_subscriber(filtered)\n", "\n", "comm.broadcast_send(\n", " 'Tip-top caravan for sale',\n", " \"Bob Jones\",\n", " 'sell.caravan')" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "*Hmm, I like me the look of that caravan.*\n", "Ok, let's send a purchase request back." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "comm.broadcast_send(\n", " 'I need a caravan for me ma',\n", " sender=\"Mickey O'Neil\",\n", " subject='purchase.caravan')" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "You can see here we don't get anything printed because we're only listening for `sell` orders.\n", "\n", "Finally let's close the communicator." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "comm.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 1 }