###質問内容
オライリー発行の「入門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.
Redisが立ち上がっていないか、ポート番号が6379以外で動いているのでは?
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
ポート番号については,どのようにすれば6379以外で行っているかを確認することが出来ますでしょうか
端末をもう一つ開くか、redis-server & としてバックグラウンドで実行すれば良いでしょう。ポート番号は何もしなくても6379で正しい番号になっています。
私はVScode上で行っているのですが,もう一つ端末を開くとは,VScodeのタブを二つにして行うということでしょうか.
あなたの回答
tips
プレビュー