Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "import zmq #import modules\n",
- "import hashlib\n",
- "import datetime\n",
- "import numpy as np\n",
- "import time\n",
- "from IPython.display import clear_output"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "#setup ZMQ publisher\n",
- "\n",
- "context = zmq.Context()\n",
- "outgoing_socket = context.socket(zmq.PUB)\n",
- "outgoing_socket.bind(\"tcp://127.0.0.1:12345\") \n",
- "# PUBLISHERS : BIND ||| SUBSCRIBERS : CONNECT"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "#sending data over ZMQ\n",
- "\n",
- "def send_data(dataset, socket):\n",
- " sha256 = hashlib.sha256()\n",
- " sha256.update(dataset.tobytes())\n",
- " hash_value = sha256.hexdigest()\n",
- " \n",
- " socket.send_multipart([\n",
- " dataset.tobytes(),\n",
- " hash_value.encode('utf-8')\n",
- " ])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2018-11-21 14:51:58.558681]\n"
- ]
- }
- ],
- "source": [
- "#main generate data and send loop\n",
- "\n",
- "transmission_interval = 5 # seconds\n",
- "while True:\n",
- " mean_of_distribution = 5 # center\n",
- " std_of_distribution = 100 # spread (width)\n",
- " number_of_samples = 1000\n",
- " dataset = np.random.normal(\n",
- " mean_of_distribution,\n",
- " std_of_distribution, \n",
- " number_of_samples\n",
- " )\n",
- " send_data(dataset, outgoing_socket)\n",
- " \n",
- " clear_output()\n",
- " print(\"[%s]\" % datetime.datetime.now())\n",
- " time.sleep(transmission_interval)"
- ]
- }
- ],
- "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.6.5"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment