Skip to content

appengine/flexible/pubsub: init publisher client globally #3280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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