Python, Twisted, Redis 및 txredisapi로 작업하십시오.txredisapi로 연결 한 후 채널을 구독하고 구독을 취소하십시오.
연결이 만들어진 후 구독 및 구독 취소를 위해 SubscriberProtocol을 어떻게 얻을 수 있습니까?
내가 SubscriberProtocol의 인스턴스를 가져와야하고 "구독"및 "구독 취소"방법을 사용할 수 있지만 가져 오는 방법을 알 수는 없다고 생각합니다.
코드 예제 :
import txredisapi as redis
class RedisListenerProtocol(redis.SubscriberProtocol):
def connectionMade(self):
self.subscribe("channelName")
def messageReceived(self, pattern, channel, message):
print "pattern=%s, channel=%s message=%s" %(pattern, channel, message)
def connectionLost(self, reason):
print "lost connection:", reason
class RedisListenerFactory(redis.SubscriberFactory):
maxDelay = 120
continueTrying = True
protocol = RedisListenerProtocol
그리고 이러한 클래스의 외부에서 :
# I need to sub/unsub from here! (not from inside de protocol)
protocolInstance = RedisListenerProtocol # Here is the problem
protocolInstance.subscribe("newChannelName")
protocolInstance.unsubscribe("channelName")
어떤 제안?
감사합니다. "defer.inlineCallbacks @"국기와 함께 함수 내에서 SubscriberProtocol의 인스턴스를 얻을 수 사용 "ClientCreator"
@defer.inlineCallbacks
def subUnsub():
deferred = yield ClientCreator(reactor, RedisListenerProtocol).connectTCP(HOST, PORT)
deferred.subscribe("newChannelName")
deferred.unsubscribe("channelName")
설명 :
다음 코드는 문제를 해결한다 지연된 데이터를 완료하기 위해 대기하는 "yield"키워드를 잊지 마십시오. 그런 다음 이것을 연기하여 구독을 취소 할 수 있습니다.
필자의 경우 yield 키워드를 잊었 기 때문에 지연이 완료되지 않았고 suscribe 및 unsubscribe 메소드가 작동하지 않았습니다.
내가 코드를 시도했지만 꼬여있는 오류를 던졌습니다 : ** exceptions.AttributeError : RedisListenerProtocol 인스턴스가 어떤 속성을 '공장'**가 그럼 난 RedisListenerProtocol에 __init __ (자체 공장을) 추가가 없습니다, 그것은 밖으로 던져 단 하나의 매개 변수에 대한 또 다른 오류가 주어졌습니다. 단순하지만 완전한 코드가 도움이 될지도 모릅니다. –