google-cloud-pubsub

504 Deadline Exceeded in google pub/sub python


from google.cloud import pubsub_v1

if __name__ == '__main__':
    publisher = pubsub_v1.PublisherClient(
        credentials="/Users/quang/.config/gcloud/application_default_credentials.json"
    )
    a = publisher.list_topics(
        timeout=10,
    )
    print(a)

Error: raise exceptions.from_grpc_error(exc) from exc

google.api_core.exceptions.DeadlineExceeded: 504 Deadline Exceeded
python 3.12.3
google-api-core 2.19.0

google-api-python-client 2.135.0

google-auth 2.29.0

google-auth-httplib2 0.2.0

google-cloud-core 2.4.1

google-cloud-firestore 2.16.0

google-cloud-pubsub 2.13.12

I am using Google Pub/Sub for my project. I have followed all the steps as outlined in the documentation. However, I am encountering a 504 error. Can someone help me?


Solution

  • You don't need to provide application_default_credentials.json to the pubsub_v1.PublisherClient.

    The client uses Application Default Credentials (ADC) and explicitly providing the credentials in this case is not only redundant but an anti-pattern.

    However, using these end-user credentials rather than e.g. a Service Account means that the client is not configured with the specific Google Project to use and this explains why the command is timing out with the 504.

    To get the code to work, you need to ensure the list_topics method receives the Google Project. There are various ways to do this. Either provide it through the environment or set it statically in your code.

    Try:

    from google.cloud import pubsub_v1
    
    PROJECT = "..." # Or os.getenv("PROJECT") and set in the enviroment
    
    if __name__ == "__main__":
        publisher = pubsub_v1.PublisherClient()
        a = publisher.list_topics(
            project=f"projects/{PROJECT}",
        )