Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. def pubsub_complete():
  2.  
  3.  
  4. project_id = "solwit-pjatk-arc-2018-gr4"
  5. topic_name = "projects/solwit-pjatk-arc-2018-gr4/topics/bookcovers"
  6. subscription_name = "bookcover"
  7.  
  8. # Instantiates a publisher and subscriber client
  9. publisher = pubsub_v1.PublisherClient()
  10. subscriber = pubsub_v1.SubscriberClient()
  11.  
  12. # The `topic_path` method creates a fully qualified identifier
  13. # in the form `projects/{project_id}/topics/{topic_name}`
  14. topic_path = subscriber.topic_path(project_id, topic_name)
  15.  
  16. # The `subscription_path` method creates a fully qualified identifier
  17. # in the form `projects/{project_id}/subscriptions/{subscription_name}`
  18. subscription_path = subscriber.subscription_path(
  19. project_id, subscription_name)
  20.  
  21. # Create the topic.
  22. topic = publisher.create_topic(topic_path)
  23. print('\nTopic created: {}'.format(topic.name))
  24.  
  25. # Create a subscription.
  26. subscription = subscriber.create_subscription(
  27. subscription_path, topic_path)
  28. print('\nSubscription created: {}\n'.format(subscription.name))
  29.  
  30. # Publish message.
  31. data = u'File size is above 10MB'
  32. # Data must be a bytestring
  33. data = data.encode('utf-8')
  34. # When you publish a message, the client returns a future.
  35. future = publisher.publish(topic_path, data=data)
  36.  
  37. def callback(message):
  38. message.ack()
  39. messages.add(message)
  40.  
  41.  
  42. # Receive messages. The subscriber is nonblocking.
  43. subscriber.subscribe(subscription_path, callback=callback)
  44.  
  45. print('\nListening for messages on {}...\n'.format(subscription_path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement