GeezerVison is showing github.com — Pub/Sub 0.28: batch support? · Issue #4141 · googleapis/google-cloud-python
Pub/Sub 0.28: batch support? #4141DescriptionIt seems that the new google-cloud-pubsub library version 0.28 does not include any support for pulling and processing messages in batches. Several of our production systems rely on processing pubsub messages in batches in order to reduce the load on other systems (e.g. to lower the amount of queries done our databases). Is there any ETA on re-adding batch pulling to the library? Activitylukesneeringer commented on Oct 12, 2017 Hi @krelst,
The new libraries relies exclusively on Pub/Sub's new streaming pull feature, in order to maximize performance in high throughput situations.
I am afraid I am unsure what you mean by "batch pulling". Can you explain your use case? krelst commented on Oct 12, 2017 Hi @lukesneeringer
Thanks for the response! In versions of the google-cloud-pubsub library bellow 0.28, the subscription.pull() method returns a list of (ack_id, messages)-tuples. We are setting the max_messages of that method to 1000, and consequently for most of our pipelines the list returned contains in between 100/200 messages. Instead of processing each message separately, we are parsing all messages in that list at once, so that we can e.g. perform all required database updates in a single query, instead of having to perform a query for each of the messages. As the google-cloud-pubsub library version 0.28 works with callbacks on a message level, this seems a lot harder to do. So for our use-case it would be useful to have callbacks for a list of messages, instead for a single message.
I hope this clarifies my question! lukesneeringer commented on Oct 12, 2017 It does clarify my question, thank you.
I see how this is a useful feature in principle, although when I have time to implement it is a separate question. The Pub/Sub team is primarily concerned with high throughput situations -- they actually explicitly wanted "polling" pull to be explicitly removed so that people would not use it by accident.
There are a few options here.
First: You could use the old polling-based pull method. It is not present in the documented surface, but you can get at it: from google.cloud.pubsub_v1.gapic.subscriber_client import SubscriberClient. That class (which should look mostly like google.cloud.pubsub_v1.subscriber.Client has a pull method that will perform the old polling behavior.
Second: You could implement the batching yourself. In fact, it would probably look pretty similar to the batching class we provide on the publisher side. Here is a quick pass at a plausible implementation (note: written in the GitHub text editor; may have some silly bugs):
import threading
class Batch(object):
def __init__(self, max_messages=250, max_seconds=30):
self._max_messages = max_messages
self._max_seconds = max_seconds
self._accepting_messages = True
self._accepting_messages_check_lock = threading.Lock()
self._messages = []
# Fire off a timer to commit when time elapses.
self._countdown_thread = threading.Thread(target=self.countdown)
self._countdown_thread.start()
@property
def accepting_messages(self):
return self._accepting_messages
def countdown(self):
time.sleep(self._max_seconds)
self._commit()
def commit(self):
threading.Thread(target=self._commit).start()
def _commit(self):
# Sanity check: Do not multiply-commit.
with self._accepting_messages_check_lock:
if not self._accepting_messages:
return
self._accepting_messages = False
# Actually do the work and then ack the messages.
do_something_with(self._messages) # <-- YOUR PROCESSING HERE.
for message in self._messages:
message.ack()
def add_message(self, message):
self._messages.append(message)
if len(self._messages) >= self._max_messages:
self._commit()
And then, your callback becomes something like:
import threading
from (somewhere) import Batch
lock = threading.Lock()
batch = None
def my_callback(message):
with lock:
if batch is None or not batch.accepting_messages:
batch = Batch()
batch.add_message(message)
A few thoughts:
- Refer to our publisher batch for an implementation of an extremely similar concept. We were worried about pluggability so what we did is more complex than what you need, but the basic principles all remain the same.
- Obviously do not rely on the scoping I did in my callback block. :-)
krelst commented on Oct 12, 2017 Indeed this would solve our issue :) As I don't think we are the only one relying on batching of message, we really hope this will be included in one of the next releases of the pub sub library. lukesneeringer commented on Oct 13, 2017 Hi @krelst,
It is something we will discuss and consider. If the need is common, we will definitely want to find a way to meet it. Taking something roughly like what I wrote above and incorporating it into the library proper could be an approach. It really depends on what people need. :-)
We are committed to the new Pub/Sub surface, so you can implement the above without worry about the surface breaking. If we do add built-in batching, it will be in an additive way that does not break this code. :-)
As you have said this is a workable solution for you, I am going to close this issue. Thanks for reaching out! john1625b commented on Mar 1, 2018 chemelnucfin commented on Mar 1, 2018 Hello, I'm currently working out the problem that you can batch larger sizes than the specified byte parameter. But this would be the setting you would use.
batch_settings = pubsub_v1.types.BatchSettings(
max_bytes=1024, # One kilobyte
) added a commit that references this issue on Oct 21, 2023This issue will close once commit c829cc3 is merged into the 'main' branch.c829cc3
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|