I am using Google Pub/Sub client v2.2.0 in python3.6 as a Subscriber.
I want my application to shutdown gracefully after acking all the messages it already received.
Sample code for a subscriber from Google's guide with minor changes that will show my issue:
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
from time import sleep
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
# The `subscription_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/subscriptions/{subscription_id}`
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message):
print(f"Received {message}.")
sleep(30)
message.ack()
print("Acked")
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
sleep(10)
streaming_pull_future.cancel()
streaming_pull_future.result()
From https://cloud.google.com/pubsub/docs/pull
I expect this code to stop pulling messages and finish the running messages and then exits.
Actually this code stops pulling messages and finish executing the running messages but it does not ack the messages. The .ack() happens but the server does not receive the ack, so next run the same messages return again.
1. Why doesn't the server receives the ack?
2. How to gracefully shutdown the subscriber?
3. What is the expected behavior of .cancel()?