如何解决云功能超时错误?

Posted

技术标签:

【中文标题】如何解决云功能超时错误?【英文标题】:How to resolve cloud functions time out error? 【发布时间】:2020-03-27 14:24:35 【问题描述】:
import json
import base64
from google.cloud import bigquery
import ast
import pandas as pd
import sys
import pandas_gbq


def process_data(data):
    #msg = str(data)
    df = pd.DataFrame("Data":data,index=[0])
    df['time'] = pd.datetime.now()
    lst = list(df)
    df[lst] = df[lst].astype(str)
    pandas_gbq.to_gbq(df,'datasetid.tableid',project_id='project_id',if_exists='append')


def receive_messages(project_id, subscription_name):
    """Receives messages from a pull subscription."""
    # [START pubsub_subscriber_async_pull]
    # [START pubsub_quickstart_subscriber]
    import time

    from google.cloud import pubsub_v1

    # TODO project_id = "Your Google Cloud Project ID"
    # TODO subscription_name = "Your Pub/Sub subscription name"

    subscriber = pubsub_v1.SubscriberClient()
    # The `subscription_path` method creates a fully qualified identifier
    # in the form `projects/project_id/subscriptions/subscription_name`
    subscription_path = subscriber.subscription_path(
        project_id, subscription_name)

    def callback(message):
        #print('Received message: '.format(message))
        process_data(message)
        message.ack()

    subscriber.subscribe(subscription_path, callback=callback)

    # The subscriber  is non-blocking. We must keep the main thread from
    # exiting to allow it to process messages asynchronously in the background.
   # print('Listening for messages on '.format(subscription_path))
    while True:
        time.sleep(60)
    # [END pubsub_subscriber_async_pull]
    # [END pubsub_quickstart_subscriber]


receive_messages(project-id,sub-id)

我正在使用云功能将实时数据从 Pub/Sub 流式传输到 bigquery。

这里出现以下错误:

部署失败: 加载用户代码时函数失败。错误消息:错误:函数加载尝试超时。

【问题讨论】:

【参考方案1】:

您的代码位于while True 循环中。 Cloud Functions 认为您的代码已崩溃,因为它没有返回。然后你的功能被杀死。

重新设计,以便 Pub/Sub 使用事件(触发器)调用您的 Cloud Functions。请按照本指南了解如何实施正确的设计:

Google Cloud Pub/Sub Triggers

【讨论】:

以上是关于如何解决云功能超时错误?的主要内容,如果未能解决你的问题,请参考以下文章

EasyNVR接入EasyNVS显示授权超时是什么原因?如何解决?

如何解决间歇性 SQL 超时错误

FeignClient 超时如何解决

如何在 erlang gen_server 中有效地使用接收子句来解决超时错误?

如何解决超时过期问题?

我如何解决错误:MongoTimeoutError:服务器选择在30000毫秒后超时,无法连接到服务器127.0.0.1:27017