REDISとPythonを用いた簡易パブサブシステム
私がここでもう一つのブログ柱でここにいるHi Guys
私はコンピュータでRedisインスタンスを起動するためにDockerを使用しています.Redisインスタンスを実行するのはとても簡単ですし、Redisインスタンスを削除するのはとても簡単です.また、楽しい部分はDockerを通して実行されているRedisはシステムに影響を与えません😄
Redisインスタンスの実行
REDISサーバの実行
私はコンピュータでRedisインスタンスを起動するためにDockerを使用しています.Redisインスタンスを実行するのはとても簡単ですし、Redisインスタンスを削除するのはとても簡単です.また、楽しい部分はDockerを通して実行されているRedisはシステムに影響を与えません😄
Redisインスタンスの実行
docker run --name demo-redis -p 6379:6379 -d redis:alpine
上記のコマンドを実行しているRedisインスタンスを停止または削除する場合は、次の操作を行うことができますdocker container stop demo-redis # <-- stop the redis instance
docker container rm demo-redis # <-- remove the redis container
出版社の作成
import redis
# initializing the redis instance
r = redis.Redis(
host='127.0.0.1',
port=6379,
decode_responses=True # <-- this will ensure that binary data is decoded
)
while True:
message = input("Enter the message you want to send to soilders: ")
r.publish("army-camp-1", message)
彼はメッセージを発行したいという話題がpublisher as a commander
であるように、軍でarmy-camp-1
を考えて、彼は郵便屋さんにメッセージを送りたいです.このアナロジーは楽しみのためだけにある😂加入者の作成
import redis
r = redis.Redis(
host='127.0.0.1',
port=6379,
decode_responses=True
)
# pubsub() method creates the pubsub object
# but why i named it mobile 🧐
# just kidding 😂 think of it as the waki taki that listens for incomming messages
mobile = r.pubsub()
# use .subscribe() method to subscribe to topic on which you want to listen for messages
mobile.subscribe('army-camp-1')
# .listen() returns a generator over which you can iterate and listen for messages from publisher
for message in mobile.listen():
print(message) # <-- you can literally do any thing with this message i am just printing it
上記のコードを実行しているデモビデオは、ここにあります
Reference
この問題について(REDISとPythonを用いた簡易パブサブシステム), 我々は、より多くの情報をここで見つけました https://dev.to/rohit20001221/simple-pub-sub-system-using-redis-and-python-25jhテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol