You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Pub/Sub Python SDK遭遇504 Deadline Exceeded错误求助

Google Pub/Sub list_topics接口报504 Deadline Exceeded错误排查

问题重现

代码示例

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)

错误信息

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

排查与解决步骤

  1. 检查网络连通性

    • 测试本地网络能否访问Google Cloud服务,可执行curl https://pubsub.googleapis.com/v1/projects/[你的项目ID]/topics替换项目ID后验证接口连通性
    • 企业网络环境下,确认防火墙或代理未拦截443端口(gRPC默认走443),或配置正确的代理参数
  2. 调整超时阈值
    当前10秒超时可能不足以应对网络延迟,尝试增大超时时间:

    a = publisher.list_topics(
        timeout=30,  # 调整为30秒或更长
    )
    
  3. 验证凭证与权限

    • 执行gcloud auth application-default login重新生成默认应用凭证,规避本地凭证文件路径或权限异常
    • 在Google Cloud控制台IAM页面,确认当前凭证关联账号拥有pubsub.topics.list权限(需绑定Pub/Sub Viewer、Editor等包含该权限的角色)
  4. 升级依赖包
    Python 3.12版本较新,尝试升级google-cloud-pubsub到最新稳定版:

    pip install --upgrade google-cloud-pubsub
    
  5. 切换至REST传输模式
    若gRPC协议存在网络兼容性问题,可强制使用REST模式测试:

    from google.cloud import pubsub_v1
    from google.api_core.client_options import ClientOptions
    
    if __name__ == '__main__':
        client_options = ClientOptions(api_endpoint="pubsub.googleapis.com:443", transport="rest")
        publisher = pubsub_v1.PublisherClient(
            credentials="/Users/quang/.config/gcloud/application_default_credentials.json",
            client_options=client_options
        )
        a = publisher.list_topics(timeout=30)
        print(a)
    

内容的提问来源于stack exchange,提问作者Quang Lê

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 13:37:06