From 54b7cf078a6509f078300a34b8f4de2977e35fd4 Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Fri, 3 Apr 2020 14:22:58 -0700 Subject: [PATCH] nit: init publisher client globally --- appengine/flexible/pubsub/main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/appengine/flexible/pubsub/main.py b/appengine/flexible/pubsub/main.py index e97597cb257..b007f252116 100644 --- a/appengine/flexible/pubsub/main.py +++ b/appengine/flexible/pubsub/main.py @@ -35,6 +35,9 @@ # Global list to storage messages received by this instance. MESSAGES = [] +# Initialize the publisher client once to avoid memory leak +# and reduce publish latency. +publisher = pubsub_v1.PublisherClient() # [START gae_flex_pubsub_index] @app.route('/', methods=['GET', 'POST']) @@ -44,9 +47,7 @@ def index(): data = request.form.get('payload', 'Example payload').encode('utf-8') - # Consider initialzing the publisher client outside this function - # for low latency publish. - publisher = pubsub_v1.PublisherClient() + # publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path( current_app.config['PROJECT'], current_app.config['PUBSUB_TOPIC'])