Guest User

Untitled

a guest
Dec 10th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import zmq #import modules\n",
  10. "import hashlib\n",
  11. "import datetime\n",
  12. "import numpy as np\n",
  13. "import time\n",
  14. "from IPython.display import clear_output"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 2,
  20. "metadata": {},
  21. "outputs": [],
  22. "source": [
  23. "#setup ZMQ publisher\n",
  24. "\n",
  25. "context = zmq.Context()\n",
  26. "outgoing_socket = context.socket(zmq.PUB)\n",
  27. "outgoing_socket.bind(\"tcp://127.0.0.1:12345\") \n",
  28. "# PUBLISHERS : BIND ||| SUBSCRIBERS : CONNECT"
  29. ]
  30. },
  31. {
  32. "cell_type": "code",
  33. "execution_count": 3,
  34. "metadata": {},
  35. "outputs": [],
  36. "source": [
  37. "#sending data over ZMQ\n",
  38. "\n",
  39. "def send_data(dataset, socket):\n",
  40. " sha256 = hashlib.sha256()\n",
  41. " sha256.update(dataset.tobytes())\n",
  42. " hash_value = sha256.hexdigest()\n",
  43. " \n",
  44. " socket.send_multipart([\n",
  45. " dataset.tobytes(),\n",
  46. " hash_value.encode('utf-8')\n",
  47. " ])"
  48. ]
  49. },
  50. {
  51. "cell_type": "code",
  52. "execution_count": null,
  53. "metadata": {},
  54. "outputs": [
  55. {
  56. "name": "stdout",
  57. "output_type": "stream",
  58. "text": [
  59. "[2018-11-21 14:51:58.558681]\n"
  60. ]
  61. }
  62. ],
  63. "source": [
  64. "#main generate data and send loop\n",
  65. "\n",
  66. "transmission_interval = 5 # seconds\n",
  67. "while True:\n",
  68. " mean_of_distribution = 5 # center\n",
  69. " std_of_distribution = 100 # spread (width)\n",
  70. " number_of_samples = 1000\n",
  71. " dataset = np.random.normal(\n",
  72. " mean_of_distribution,\n",
  73. " std_of_distribution, \n",
  74. " number_of_samples\n",
  75. " )\n",
  76. " send_data(dataset, outgoing_socket)\n",
  77. " \n",
  78. " clear_output()\n",
  79. " print(\"[%s]\" % datetime.datetime.now())\n",
  80. " time.sleep(transmission_interval)"
  81. ]
  82. }
  83. ],
  84. "metadata": {
  85. "kernelspec": {
  86. "display_name": "Python 3",
  87. "language": "python",
  88. "name": "python3"
  89. },
  90. "language_info": {
  91. "codemirror_mode": {
  92. "name": "ipython",
  93. "version": 3
  94. },
  95. "file_extension": ".py",
  96. "mimetype": "text/x-python",
  97. "name": "python",
  98. "nbconvert_exporter": "python",
  99. "pygments_lexer": "ipython3",
  100. "version": "3.6.5"
  101. }
  102. },
  103. "nbformat": 4,
  104. "nbformat_minor": 2
  105. }
Add Comment
Please, Sign In to add comment