GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Pubsub: Consider a blocking publisher to prevent OOMing · Issue #16 · googleapis/python-pubsub
This repository was archived by the owner on Oct 13, 2026. It is now read-only.
This repository was archived by the owner on Oct 13, 2026. It is now read-only.
Pubsub: Consider a blocking publisher to prevent OOMing #16Copy link Copy link Closed Labels api: pubsubIssues related to the googleapis/python-pubsub API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design. DescriptionPublishing medium-sized (32 kiB) messages in a loop causes the process's memory usage to quickly run out. I set a 2 GiB memory limit, and the process runs out of memory after publishing about 20000 messages. I believe there are two issues:
As far as I can see, there no good way to use the current client library to publish large batches of messages without HUGE memory consumption. I believe this can be fixed in two ways: Reduce memory of the current library:
Back PressureChange the publisher to keep a queue of batches, and block after there are more than N in flight at once. I implemented a client that takes the blocking queue approach. It can publish this workload without ever exceeding 300 MiB of memory. Only 3 in-flight batches were necessary to have the process be CPU bound when running in Google Cloud, so I don't think the queue doesn't even need to be too large. Environment detailsOS: Linux, ContainerOS (GKE), Container is Debian9 (using distroless) Steps to reproduce
Code example#!/usr/bin/env python3 ''' Create the topic and a subscription: gcloud pubsub topics create deleteme_publish_memleak_test gcloud pubsub subscriptions create deleteme_memleak_subscription --topic=deleteme_publish_memleak_test ''' from google.cloud import pubsub_v1 import argparse import google.auth import logging import time def main(): parser = argparse.ArgumentParser(description='mass publish to pub/sub') parser.add_argument('--topic', default='deleteme_publish_memleak_test', help='topic to publish to') parser.add_argument('--messages', type=int, default=200000, help='number of messages to publish') parser.add_argument('--bytes_per_message', type=int, default=32 * 1024, help='bytes per message to publish') parser.add_argument('--log_every_n', type=int, default=2000, help='log every N messages') args = parser.parse_args() credentials, project_id = google.auth.default() publish_client = pubsub_v1.PublisherClient(credentials=credentials) topic_path = publish_client.topic_path(project_id, args.topic) logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s: %(message)s', ) logging.info('publishing %d messages to topic %s; %d bytes per message; %.1f total MiB', args.messages, topic_path, args.bytes_per_message, (args.messages * args.bytes_per_message) / 1024. / 1024.) start = time.time() for i in range(args.messages): message = (i % 256).to_bytes(1, byteorder='little') * args.bytes_per_message publish_client.publish(topic_path, message) if i % args.log_every_n == 0: logging.info('published %d messages', i) end = time.time() logging.info('done publishing in %f s', end - start) if __name__ == '__main__': main() Activity
MetadataMetadataAssigneesLabelsapi: pubsubIssues related to the googleapis/python-pubsub API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design. TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |