[Python] Clean way to limit the subscribers in the Publish & Subscribe model

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    In the Publish & Subscribe model using autobahn, I would like to limit the number of subscribers for a given @exportSub(...). How do you know the number of subscribers?

    (From examples)

    class MyTopicService(object):

    def __init__(self, allowedTopicIds):
    self.allowedTopicIds = allowedTopicIds

    @exportSub("", True)
    def subscribe(self, topicUriPrefix, topicUriSuffix):
    ret = False
    print "client wants to subscribe to %s %s. Allowed topic ids:%s" % (topicUriPrefix, topicUriSuffix, self.allowedTopicIds)
    try:
    if topicUriSuffix in self.allowedTopicIds:
    ret = True
    print "Subscribing client to topic %s %s" % (topicUriPrefix, topicUriSuffix)
    else:
    print "Client not allowed to subscribe to topic %s %s" % (topicUriPrefix, topicUriSuffix)
    except:
    print "illegal topic - skipped subscription"
    finally:
    return ret

    class MyServerProtocol(WampServerProtocol):
    def onSessionOpen(self):
    self.registerHandlerForPubSub(MyTopicService(my_keys_1), url_1_foo)
    self.registerHandlerForPubSub(MyTopicService(my_keys_2), url_2_bar)


    I could probably do this using my own WampServerFactory, overriding the onClientSubscribed and onClientUnsubscribed methods and using an internal array variable... But I would like to know if there is a cleaner way...

    class MyFactory(WampServerFactory):
    def onClientSubscribed(self, *a, **k):
    WampServerFactory.onClientSubscribed(self, a, k)
    print '=== client subscribed '

    def onClientUnsubscribed(self, *a, **k):
    WampServerFactory.onClientUnsubscribed(self, a, k)
    print '=== client unsubscribed '


    Code can be found here.

    Continue reading...

Compartilhe esta Página