虽然等待功能未完成 运行 循环更新云 Pub/Sub 确认截止日期

While awaited function not finished run loop to update Cloud Pub/Sub acknolwedgement deadline

我的所有消息都将超过 acknowledging/processing 来自 Cloud PubSub 的 600 秒限制,因此当我处理消息时,我需要计算最多 590 秒并请求延长确认截止日期另外 600 秒(这样另一个订阅者就不会重新提交)。我的计划是编写一个包装器等待 运行 和

的处理
while awaited processing_function not finished:
    update the deadline

我要处理的基本代码:

subscriber = pubsub_v1.SubscriberClient()
subscription_path = "projects/path/to/subscription"

def callback(message):
    print(f'Received message: {message}')
    print(f'data: {message.data}')

    if message.attributes:
        # PROCESS THAT'S GOING TO TAKE A LONG TIME
        # ...

    message.ack()

# subscribe method provides an asynchronous interface for processing its callback
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
with subscriber:                                           # wrap subscriber in a 'with' block to automatically call close() when done
    try:
        streaming_pull_future.result()                          # going without a timeout will wait & block indefinitely
    except TimeoutError:
        streaming_pull_future.cancel()                          # trigger the shutdown
        streaming_pull_future.result()

我如何实施我的计划以持续更新截止日期直到回调完成 运行ning?我在 asyncio 方面不是很有经验。我什至可以拥有一个等待回调的包装函数,并且 运行 在等待时将 while 循环作为后台进程吗?

official client source code 表示“默认实现会为您处理此问题;您不需要手动处理设置 ack 截止日期。”