Q:

What happens if I subscribe to the same topic multiple times? (Python, Google Pubsub)

If I have the following code, will anything bad happen? Will it try to create new subscriptions? Is subscribe an idempotent operation?
subscriber = pubsub_v1.SubscriberClient()

def f(msg):
    print(msg.data)
    print(msg)
    msg.ack()

def create_subscriptions():
  restults = [] # some sql query
  for result in results:
    path = self.subscriber.subscription_path('PROJECT', result)
    subscriber.subscribe(self.path, callback=f)

while True:
    time.sleep(60)
    create_subscriptions()

I need to be able to update my subscriptions based on when people create new ones. Is there any problem with this approach?

A:

You should avoid repeatedly calling “subscribe” for the same subscription -- even though you will most likely not increase the number of duplicate messages that are delivered, you would create multiple instances of the receiving infrastructure. This is both inefficient, and defeats some of the flow control properties that Pub/Sub provides, since these are only computed per instance of the subscriber; i.e. it can cause your subscriber job to run out of memory and fail, for example.
Instead, I would suggest keeping track of which subscribers you’ve already created. Note that the “subscribe” method returns a future that you can use for this purpose, or to cancel the message receiving when necessary. You can find more details on the documentation.

