質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Redis

Redisは、オープンソースのkey-valueデータストアで、NoSQLに分類されます。すべてのデータをメモリ上に保存するため、処理が極めて高速です。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

786閲覧

Redisでパブサブシステムを作るとエラーが起きる

grape_ll

総合スコア83

Redis

Redisは、オープンソースのkey-valueデータストアで、NoSQLに分類されます。すべてのデータをメモリ上に保存するため、処理が極めて高速です。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/03/23 06:41

###質問内容
オライリー発行の「入門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.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kenji.Noguchi

2021/03/23 23:53

Redisが立ち上がっていないか、ポート番号が6379以外で動いているのでは?
grape_ll

2021/03/24 06:16

Redisを立ち上げるとは,redis-serverのことでしょうか.先ほど調べたので質問時点では行っていませんでした.実行してみたところ,下記のようになってしまって,python3 pub.pyのような実行が出来なくってしまいました. redis-server 687:C 24 Mar 2021 15:11:00.658 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 687:C 24 Mar 2021 15:11:00.658 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=687, just started 687:C 24 Mar 2021 15:11:00.658 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 687 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 687:M 24 Mar 2021 15:11:00.661 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 687:M 24 Mar 2021 15:11:00.661 # Server initialized 687:M 24 Mar 2021 15:11:00.661 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 687:M 24 Mar 2021 15:11:00.662 * DB loaded from disk: 0.001 seconds 687:M 24 Mar 2021 15:11:00.662 * Ready to accept connections
grape_ll

2021/03/24 06:17

ポート番号については,どのようにすれば6379以外で行っているかを確認することが出来ますでしょうか
Kenji.Noguchi

2021/03/25 04:23 編集

端末をもう一つ開くか、redis-server & としてバックグラウンドで実行すれば良いでしょう。ポート番号は何もしなくても6379で正しい番号になっています。
grape_ll

2021/03/25 06:35

私はVScode上で行っているのですが,もう一つ端末を開くとは,VScodeのタブを二つにして行うということでしょうか.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問