Guest User

Untitled

a guest
Jan 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 88,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import redis\n",
  10. "import time\n",
  11. "import traceback"
  12. ]
  13. },
  14. {
  15. "cell_type": "code",
  16. "execution_count": 89,
  17. "metadata": {},
  18. "outputs": [],
  19. "source": [
  20. "r = redis.Redis(host='localhost', port=6379, db=0)\n"
  21. ]
  22. },
  23. {
  24. "cell_type": "code",
  25. "execution_count": 90,
  26. "metadata": {},
  27. "outputs": [
  28. {
  29. "data": {
  30. "text/plain": [
  31. "b'bar'"
  32. ]
  33. },
  34. "execution_count": 90,
  35. "metadata": {},
  36. "output_type": "execute_result"
  37. }
  38. ],
  39. "source": [
  40. "r.set('foo', 'bar')\n",
  41. "r.get('foo')"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": 91,
  47. "metadata": {},
  48. "outputs": [
  49. {
  50. "data": {
  51. "text/plain": [
  52. "b'5'"
  53. ]
  54. },
  55. "execution_count": 91,
  56. "metadata": {},
  57. "output_type": "execute_result"
  58. }
  59. ],
  60. "source": [
  61. "r.set('count',1)\n",
  62. "r.incr('count')\n",
  63. "r.incr('count')\n",
  64. "r.incr('count')\n",
  65. "r.incr('count')\n",
  66. "r.get('count')"
  67. ]
  68. },
  69. {
  70. "cell_type": "code",
  71. "execution_count": 92,
  72. "metadata": {},
  73. "outputs": [
  74. {
  75. "data": {
  76. "text/plain": [
  77. "3"
  78. ]
  79. },
  80. "execution_count": 92,
  81. "metadata": {},
  82. "output_type": "execute_result"
  83. }
  84. ],
  85. "source": [
  86. "r.rpush('Artists', 'Three days grace')\n",
  87. "r.rpush('Artists', '30 Seconds To Mars')\n",
  88. "r.rpush('Artists', 'starset')\n",
  89. "r.llen('Artists')\n"
  90. ]
  91. },
  92. {
  93. "cell_type": "code",
  94. "execution_count": 93,
  95. "metadata": {},
  96. "outputs": [],
  97. "source": [
  98. "r.lindex('Artists', 3)"
  99. ]
  100. },
  101. {
  102. "cell_type": "code",
  103. "execution_count": 94,
  104. "metadata": {},
  105. "outputs": [
  106. {
  107. "name": "stdout",
  108. "output_type": "stream",
  109. "text": [
  110. "b'Three days grace'\n",
  111. "b'30 Seconds To Mars'\n",
  112. "b'starset'\n"
  113. ]
  114. }
  115. ],
  116. "source": [
  117. "while(r.llen('Artists') != 0):\n",
  118. "\n",
  119. " print(r.lpop('Artists'))\n"
  120. ]
  121. },
  122. {
  123. "cell_type": "markdown",
  124. "metadata": {},
  125. "source": [
  126. "## Pub/Sub"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": 95,
  132. "metadata": {},
  133. "outputs": [],
  134. "source": [
  135. "def RedisCheck():\n",
  136. " p = r.pubsub() \n",
  137. " p.subscribe('startScripts') \n",
  138. " PAUSE = True\n",
  139. " red = redis.StrictRedis(host='localhost', port=6379)\n",
  140. " \n",
  141. " while PAUSE: \n",
  142. " print(\"Waiting For redisStarter...\")\n",
  143. " message = p.get_message() \n",
  144. " if message:\n",
  145. " command = message['data']\n",
  146. " if command == b'START':\n",
  147. " PAUSE = False\n",
  148. " time.sleep(1)\n",
  149. " \n",
  150. " print(\"Permission to start :)\")\n",
  151. " \n",
  152. " "
  153. ]
  154. },
  155. {
  156. "cell_type": "code",
  157. "execution_count": 96,
  158. "metadata": {},
  159. "outputs": [],
  160. "source": [
  161. "def WorkCheck():\n",
  162. "\n",
  163. " r = redis.StrictRedis(host='localhost', port=6379) \n",
  164. "\n",
  165. " p = r.pubsub() \n",
  166. "\n",
  167. " print(\"Starting main scripts...\")\n",
  168. "\n",
  169. " r.publish('startScripts', 'START') \n",
  170. " print(\"Done\")\n"
  171. ]
  172. },
  173. {
  174. "cell_type": "code",
  175. "execution_count": null,
  176. "metadata": {},
  177. "outputs": [],
  178. "source": []
  179. }
  180. ],
  181. "metadata": {
  182. "kernelspec": {
  183. "display_name": "Python 3",
  184. "language": "python",
  185. "name": "python3"
  186. },
  187. "language_info": {
  188. "codemirror_mode": {
  189. "name": "ipython",
  190. "version": 3
  191. },
  192. "file_extension": ".py",
  193. "mimetype": "text/x-python",
  194. "name": "python",
  195. "nbconvert_exporter": "python",
  196. "pygments_lexer": "ipython3",
  197. "version": "3.6.5"
  198. }
  199. },
  200. "nbformat": 4,
  201. "nbformat_minor": 2
  202. }
Add Comment
Please, Sign In to add comment