###質問内容
オライリー発行の「入門Python3」を学んでいるのですが,p.349にある,Redisを用いたパブサブシステムをそのまま写したのにエラーが吐かれてしまい,うまく動作しません.どのようにすればエラーを解決することが出来ますでしょうか.
pip3 install redisは行いました.
実行結果とコードは以下に載せます
###pub.py
Python
1import redis 2import random 3 4conn = redis.Redis() 5cats = ["siamese", "persian", "maine coon", "norweigan forest"] 6hats = ["stovepipe", "bowler", "tam-o-shnter", "fadora"] 7for msg in range(10): 8 cat = random.choice(cats) 9 hat = random.choice(hats) 10 print("Publish: %s wears a %s" % (cat, hat)) 11 conn.publish(cat, hat)
###sub.py
Python
1import redis 2 3conn = redis.Redis() 4 5topics = ["maine coon", "persian"] 6sub = conn.pubsub() 7sub.subscribe(topics) 8for msg in sub.listen(): 9 if msg["type"] == "message": 10 cat = msg["channel"] 11 hat = msg["data"] 12 print("Subscribe: %s wears a %s" % (cat, hat))
###実行結果
~/work/python/ch11$ python3 sub.py Traceback (most recent call last): File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 559, in connect sock = self._connect() File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 615, in _connect raise err File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 603, in _connect sock.connect(socket_address) ConnectionRefusedError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "sub.py", line 7, in <module> sub.subscribe(topics) File "/home/.local/lib/python3.8/site-packages/redis/client.py", line 3580, in subscribe ret_val = self.execute_command('SUBSCRIBE', *iterkeys(new_channels)) File "/home/.local/lib/python3.8/site-packages/redis/client.py", line 3466, in execute_command self.connection = self.connection_pool.get_connection( File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 1192, in get_connection connection.connect() File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 563, in connect raise ConnectionError(self._error_message(e)) redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused. ~/work/python/ch11$ python3 pub.py Publish: persian wears a bowler Traceback (most recent call last): File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 559, in connect sock = self._connect() File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 615, in _connect raise err File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 603, in _connect sock.connect(socket_address) ConnectionRefusedError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "pub.py", line 11, in <module> conn.publish(cat, hat) File "/home/.local/lib/python3.8/site-packages/redis/client.py", line 3098, in publish return self.execute_command('PUBLISH', channel, message) File "/home/.local/lib/python3.8/site-packages/redis/client.py", line 898, in execute_command conn = self.connection or pool.get_connection(command_name, **options) File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 1192, in get_connection connection.connect() File "/home/.local/lib/python3.8/site-packages/redis/connection.py", line 563, in connect raise ConnectionError(self._error_message(e)) redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused.
あなたの回答
tips
プレビュー