对不起,如果这是一个愚蠢的问题,但是在过去的两个星期中,这一直困扰着我,我不得不最后问。我正在尝试听我从Python的Node JS express发送的redis发布消息。我看到有两种可能
import redis
import threading
import time
client = redis.Redis()
subscriber = client.pubsub()
subscriber.subscribe("klines")
class ThreadedSubscriber1(threading.Thread):
"""
This class handles subscription messages from redis inside a separate thread in its run() method
"""
def __init__(self):
super().__init__()
def run(self):
for message in subscriber.listen():
print(message)
def main1():
try:
s = ThreadedSubscriber1()
s.start()
s.join()
except KeyboardInterrupt:
pass
main1()
class ThreadedSubscriber2(threading.Thread):
"""
This class handles subscription messages from redis inside a separate thread in its run() method
"""
def __init__(self):
super().__init__()
def run(self):
for message in subscriber.listen():
print(message)
def main2():
try:
s = ThreadedSubscriber2()
s.setDaemon(True)
s.start()
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
main2()