Python:在特定机器上发布订阅redis通道的运行线程
我正在centos 6.5上运行这段代码。同样的代码也适用于我的另一台机器,它也有Centos 6.5。我不明白这怎么可能:Python:在特定机器上发布订阅redis通道的运行线程,python,redis,Python,Redis,我正在centos 6.5上运行这段代码。同样的代码也适用于我的另一台机器,它也有Centos 6.5。我不明白这怎么可能: import redis import threading import time # Singleton class class Listener(threading.Thread): __shared_state = {} def __init__(self, r): print 'Initing listener'
import redis
import threading
import time
# Singleton class
class Listener(threading.Thread):
__shared_state = {}
def __init__(self, r):
print 'Initing listener'
self.__dict__ = self.__shared_state
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
def list_subscribe(self, channels):
self.pubsub.subscribe(channels)
def list_unsubscribe(self, channels):
self.pubsub.unsubscribe(channels)
def work(self, item):
print item['channel'], ":", item['data']
def run(self):
print 'Running thread'
for item in self.pubsub.listen():
if item['data'] == "KILL":
self.pubsub.unsubscribe()
print self, "unsubscribed and finished"
break
else:
print 'Got something'
self.work(item)
if __name__ == "__main__":
r = redis.Redis()
client = Listener(r)
client.start()
client.list_subscribe(['test'])
for x in range(0, 10):
r.publish('test', 'this will reach the listener')
time.sleep(1)
r.publish('test', 'KILL')
我根本没看到印刷发生。我还添加了断言(未显示),我没有命中任何断言
这是我看到的输出
$ python redis_listener.py
Initing listener
Running thread
这是从redis客户端发送的,以确认数据实际上正在发布
1425758220.317547 [0 [::1]:36491] "SUBSCRIBE" "test"
1425758220.318266 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758221.319827 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758222.321659 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758223.323464 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758224.325268 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758225.327022 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758226.328722 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758227.329919 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758228.331199 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758229.332095 [0 [::1]:36492] "PUBLISH" "test" "this will reach the listener"
1425758230.333751 [0 [::1]:36492] "PUBLISH" "test" "KILL"
同一代码怎么可能在一台机器上工作而在另一台机器上不工作
这是它“工作”的机器的输出
初始化侦听器
螺纹
有东西吗
测试:1
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
有东西吗
测试:这将到达侦听器
取消订阅并完成
Initing listener
Running thread
Got something
test : 1
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
Got something
test : this will reach the listener
<Listener(Thread-1, started 139825429100288)> unsubscribed and finished