|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
17 | 17 |
|
18 |
| -def run_quickstart(): |
19 |
| - # [START pubsub_quickstart_create_topic] |
20 |
| - # Imports the Google Cloud client library |
| 18 | +import argparse |
| 19 | + |
| 20 | + |
| 21 | +def end_to_end(project_id, topic_name, subscription_name, num_messages): |
| 22 | + # [START pubsub_end_to_end] |
| 23 | + import time |
| 24 | + |
21 | 25 | from google.cloud import pubsub_v1
|
22 | 26 |
|
23 |
| - # Instantiates a client |
| 27 | + # TODO project_id = "Your Google Cloud Project ID" |
| 28 | + # TODO topic_name = "Your Pub/Sub topic name" |
| 29 | + # TODO num_messages = number of messages to test end-to-end |
| 30 | + |
| 31 | + # Instantiates a publisher and subscriber client |
24 | 32 | publisher = pubsub_v1.PublisherClient()
|
| 33 | + subscriber = pubsub_v1.SubscriberClient() |
| 34 | + |
| 35 | + # The `topic_path` method creates a fully qualified identifier |
| 36 | + # in the form `projects/{project_id}/topics/{topic_name}` |
| 37 | + topic_path = subscriber.topic_path(project_id, topic_name) |
25 | 38 |
|
26 |
| - # The resource path for the new topic contains the project ID |
27 |
| - # and the topic name. |
28 |
| - topic_path = publisher.topic_path( |
29 |
| - 'my-project', 'my-new-topic') |
| 39 | + # The `subscription_path` method creates a fully qualified identifier |
| 40 | + # in the form `projects/{project_id}/subscriptions/{subscription_name}` |
| 41 | + subscription_path = subscriber.subscription_path( |
| 42 | + project_id, subscription_name) |
30 | 43 |
|
31 | 44 | # Create the topic.
|
32 | 45 | topic = publisher.create_topic(topic_path)
|
| 46 | + print('\nTopic created: {}'.format(topic.name)) |
| 47 | + |
| 48 | + # Create a subscription. |
| 49 | + subscription = subscriber.create_subscription( |
| 50 | + subscription_path, topic_path) |
| 51 | + print('\nSubscription created: {}\n'.format(subscription.name)) |
| 52 | + |
| 53 | + publish_begin = time.time() |
| 54 | + |
| 55 | + # Publish messages. |
| 56 | + for n in range(num_messages): |
| 57 | + data = u'Message number {}'.format(n) |
| 58 | + # Data must be a bytestring |
| 59 | + data = data.encode('utf-8') |
| 60 | + # When you publish a message, the client returns a future. |
| 61 | + future = publisher.publish(topic_path, data=data) |
| 62 | + print('Published {} of message ID {}.'.format(data, future.result())) |
| 63 | + |
| 64 | + publish_time = time.time() - publish_begin |
33 | 65 |
|
34 |
| - print('Topic created: {}'.format(topic)) |
35 |
| - # [END pubsub_quickstart_create_topic] |
| 66 | + messages = set() |
| 67 | + |
| 68 | + def callback(message): |
| 69 | + print('Received message: {}'.format(message)) |
| 70 | + # Unacknowledged messages will be sent again. |
| 71 | + message.ack() |
| 72 | + messages.add(message) |
| 73 | + |
| 74 | + subscribe_begin = time.time() |
| 75 | + |
| 76 | + # Receive messages. The subscriber is nonblocking. |
| 77 | + subscriber.subscribe(subscription_path, callback=callback) |
| 78 | + |
| 79 | + print('\nListening for messages on {}...\n'.format(subscription_path)) |
| 80 | + |
| 81 | + while True: |
| 82 | + if len(messages) == num_messages: |
| 83 | + subscribe_time = time.time() - subscribe_begin |
| 84 | + print("\nReceived all messages.") |
| 85 | + print("Publish time lapsed: {:.2f}s.".format(publish_time)) |
| 86 | + print("Subscribe time lapsed: {:.2f}s.".format(subscribe_time)) |
| 87 | + break |
| 88 | + else: |
| 89 | + # Sleeps the thread at 50Hz to save on resources. |
| 90 | + time.sleep(1. / 50) |
| 91 | + # [END pubsub_end_to_end] |
36 | 92 |
|
37 | 93 |
|
38 | 94 | if __name__ == '__main__':
|
39 |
| - run_quickstart() |
| 95 | + |
| 96 | + parser = argparse.ArgumentParser( |
| 97 | + description=__doc__, |
| 98 | + formatter_class=argparse.RawDescriptionHelpFormatter |
| 99 | + ) |
| 100 | + parser.add_argument('project_id', help='Your Google Cloud project ID') |
| 101 | + parser.add_argument('topic_name', help='Your topic name') |
| 102 | + parser.add_argument('subscription_name', help='Your subscription name') |
| 103 | + parser.add_argument('num_msgs', type=int, help='Number of test messages') |
| 104 | + |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + end_to_end(args.project_id, args.topic_name, args.subscription_name, |
| 108 | + args.num_msgs) |
0 commit comments