Redisを使ってのパブサブシステム
入門python3の349ページで下記のような二つのコードを書いているんですがエラーが出ます
これはどういうエラーなんでしょうか? 並列処理のところのプログラムで同じようなエラーが出ます。
●redis_pub.py
import redis
import random
conn = redis.Redis()
cats = ['siamese', 'persian', 'maine conn', 'norwegian forest']
hats = ['stovepipe', 'boowler', 'tam-o-shanter', 'fedora']
for msg in range(10):
cat = random.choice(cats)
hat = random.choice(hats)
print('Publish: %s wears a %s' % (cat, hat))
conn.publish(cat, hat)
●redis_sub.py
import redis
conn = redis.Redis()
topics = ['maine coon', 'persian']
sub = conn.pubsub()
sub.subscribe(topics)
for msg in sub.listen():
if msg['type'] == 'message':
cat = msg['channel']
hat = msg['data']
print('Subscribe: %s wears a %s' % (cat, hat))
ターミナルで起動するとこういうエラーが出ます
$ python redis_sub.py
Traceback (most recent call last):
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 439, in connect
sock = self._connect()
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 494, in _connect
raise err
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 482, in _connect
sock.connect(socket_address)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/client.py", line 2165, in _execute
return command(*args)
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 563, in send_command
self.send_packed_command(self.pack_command(*args))
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 538, in send_packed_command
self.connect()
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 61 connecting to localhost:6379. Connection refused.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 439, in connect
sock = self._connect()
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 494, in _connect
raise err
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 482, in _connect
sock.connect(socket_address)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "redis_sub.py", line 6, in <module>
sub.subscribe(topics)
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/client.py", line 2229, in subscribe
ret_val = self.execute_command('SUBSCRIBE', *iterkeys(new_channels))
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/client.py", line 2161, in execute_command
self._execute(connection, connection.send_command, *args)
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/client.py", line 2172, in _execute
connection.connect()
File "/Users/tadashintaro/anaconda/lib/python3.6/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 61 connecting to localhost:6379. Connection refused.