Skip to content

Commit efe5e78

Browse files
nit: init publisher client globally (#3280)
1 parent a9946c2 commit efe5e78

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

appengine/flexible/pubsub/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
# Global list to storage messages received by this instance.
3636
MESSAGES = []
3737

38+
# Initialize the publisher client once to avoid memory leak
39+
# and reduce publish latency.
40+
publisher = pubsub_v1.PublisherClient()
3841

3942
# [START gae_flex_pubsub_index]
4043
@app.route('/', methods=['GET', 'POST'])
@@ -44,9 +47,7 @@ def index():
4447

4548
data = request.form.get('payload', 'Example payload').encode('utf-8')
4649

47-
# Consider initialzing the publisher client outside this function
48-
# for low latency publish.
49-
publisher = pubsub_v1.PublisherClient()
50+
# publisher = pubsub_v1.PublisherClient()
5051
topic_path = publisher.topic_path(
5152
current_app.config['PROJECT'],
5253
current_app.config['PUBSUB_TOPIC'])

0 commit comments

Comments
 (0)