Skip to content

Commit

Permalink
nit: init publisher client globally (GoogleCloudPlatform#3280)
Browse files Browse the repository at this point in the history
  • Loading branch information
anguillanneuf authored Apr 3, 2020
1 parent a9946c2 commit efe5e78
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions appengine/flexible/pubsub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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'])
Expand Down

0 comments on commit efe5e78

Please sign in to comment.