Python 谷歌云功能使用pubsub消息启动其他GCF,以';状态:连接错误';

Python 谷歌云功能使用pubsub消息启动其他GCF,以';状态:连接错误';,python,google-cloud-functions,google-cloud-pubsub,Python,Google Cloud Functions,Google Cloud Pubsub,情况: 我有两个google云函数,我们称它们为gcf composer和gcf action 我有一个70000个我想要执行gcf操作的唯一DICT的列表 我在所有dict上使用gcf composer循环,并将每个dict的消息发布到包含dict作为有效负载的gcf操作主题 我需要gcf编写器,因为直接为所有DICT运行gcf操作需要9分钟以上的时间 我使用googlecloudscheduler启动gcf编写器 问题 在云上启动gcf composer时,x秒后,它将停止并返回以下内容

情况:

  • 我有两个google云函数,我们称它们为gcf composer和gcf action
  • 我有一个70000个我想要执行gcf操作的唯一DICT的列表
  • 我在所有dict上使用gcf composer循环,并将每个dict的消息发布到包含dict作为有效负载的gcf操作主题
  • 我需要gcf编写器,因为直接为所有DICT运行gcf操作需要9分钟以上的时间
  • 我使用googlecloudscheduler启动gcf编写器
问题
在云上启动gcf composer时,x秒后,它将停止并返回以下内容:

这些是4次单独尝试的结果

为什么它会给我“已完成状态:'连接错误'”,我该如何解决它
当我在本地运行此功能,以便向主题发送消息时,它会起作用。

如果您需要更多代码或信息,请告诉我

gcf作曲家的代码

from mlibs.pubsub import PubSubConnection
pubsub = PubSubConnection()
TOPIC_NAME = 'gcf-action-topic'

def gcf_composer(period, run_method='local', list_x, names_y):
"""Run composer given run method (local or cloud_fn)"""

for k, row in names_y.iterrows():
    # get dict of identifiers
    y = row.to_dict()

    for x in list_x:
            parameters = {'x': x, 'y': y}
            if run_method == 'local':
                c.test_local(x=x, y=y)
            elif run_method == 'cloud-fn':
                pubsub.publish_to_topic(topic_name=TOPIC_NAME, params={'params': parameters})
            else:
                print(f'Unknown run method {run_method} used. Please try again.')
公共连接:

"""Interaction with the Pub/Sub Engine"""
from google.oauth2 import service_account
from google.cloud import pubsub_v1

from mlibs.utils import json
from mlibs.utils import decode
from mlibs.utils import files as fs


class PubSubConnection:

    def __init__(self):
        """Initiate a PubSub connection"""
        self.project_id = None
        self.publisher = None
        self.count = 0

        self.init_connection()

    def init_connection(self):
        """Initiates a connection given the service account"""
        self.publisher = pubsub_v1.PublisherClient(credentials=*****)
        self.project_id = credentials.project_id

    def publish_to_topic(self, topic_name, params):

        # Define the topic path
        topic_path = self.publisher.topic_path(self.project_id, topic_name)

        # Convert to ByteString
        params_bytes = json.dumps(params).encode('utf-8')

        # Publish and handle the Future
        cbl = Callable(self.count, params)
        message_future = self.publisher.publish(topic_path, data=params_bytes, test='pubsub')

        # Done callback
        message_future.add_done_callback(cbl.callback)

        # https://googleapis.dev/python/pubsub/latest/publisher/index.html#futures
        # block result
        # message_id = message_future.result()
        self.count = self.count + 1

        print(f'[pubsub][{self.count}][{topic_name}]')


class Callable:
    def __init__(self, count, params):
        self.count = count
        self.params = params

    def callback(self, message_future):
        if message_future.exception(timeout=30):
            print(f'[pubsub-except] Publishing message threw an Exception {message_future.exception()}')
        else:
            print(f'[pubsub][{self.count}][{message_future.result()}] {self.params}')

2个问题:PubSubConnection的功能是什么?在连接错误消息出现前的前几秒钟内,您是否在pubSub主题中发布了消息?@guillaumeblaquiere添加了PubSubConnection函数。是的,因此在连接错误发生之前,gcf操作get至少被调用了200-300次。