2
import redis
import threading
class Listener(threading.Thread):
def __init__(self, r, channel):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channel)
def run(self):
for item in self.pubsub.listen():
# do stuff
pass
在上面的代碼我怎麼停止線程?蟒蛇redis pubsub阻止
下面我有一個示例代碼來告訴你我想什麼:
class Listener(threading.Thread):
def __init__(self, r, channel):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channel)
self.stop = False
def run(self):
while not stop:
# get item from channel
所以當屬性停止==真該線程將退出循環,並結束。 這可能嗎?如果不是什麼選擇?
這裏是ioloop終止無阻塞例如一旦已收到消息 - https://github.com /abhinavsingh/async_pubsub/blob/master/examples/redis_subscribe.py#L27 –