首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用python异步运行2个云函数?

如何使用python异步运行2个云函数?
EN

Stack Overflow用户
提问于 2019-11-04 18:12:32
回答 2查看 522关注 0票数 1

我有3个用python编写的GCP云函数,分别是CF1,CF2,CF3。CF1会检查某些条件,因此它应该并行执行CF2和CF3。

我试过了

代码语言:javascript
运行
复制
if condition is true:
    requests.get("url of CF2")
    print("CF2 executed successfully")
    requests.get("url of CF3")
    print("CF3 executed successfully")

CF1代码:

代码语言:javascript
运行
复制
import requests

static_query = "select * from `myproject.mydataset.mytable`"
    try:

        # Executing query and loading data into temporary table.
        client = bigquery.Client()
        job_config = bigquery.QueryJobConfig()
        dest_dataset = client.dataset(temporary_dataset, temporary_project)
        dest_table = dest_dataset.table(temporary_table)
        job_config.destination = dest_table
        job_config.create_disposition = 'CREATE_IF_NEEDED'
        job_config.write_disposition = 'WRITE_TRUNCATE'
        query_job = client.query(static_query, location=bq_location, job_config=job_config)
        query_job.result()
        table = client.get_table(dest_table)
        expiration = (datetime.now() + timedelta(minutes=expiration_time))
        table.expires = expiration
        table = client.update_table(table, ["expires"])
        logging.info("Query result loaded into temporary table: {}".format(temporary_table))

        # Check row count of resultant query from temporary table.
        count_query = "select count(*) size from `{}.{}.{}`".format(temporary_project, temporary_dataset,
                                                                    temporary_table)
        job = client.query(count_query)
        results = job.result()
        count = 0
        for row in results:
            count = row.size

        # If row count of query result is empty log error message on stack-driver.
        if count == 0:
            logging.error("Query executed with empty result set.")

        # If row count of query result has records then trigger below two cloud functions (this should be parallel execution).
        else:
            # Trigger CF2 cloud function.
            requests.get("{}".format(cf2_endpoint))
            logging.info("CF2 executed successfully.")

            # Trigger CF3 cloud function.
            requests.get("{}".format(cf3_endpoint))
            logging.info("CF3 executed successfully.")
    except RuntimeError:
        logging.error("Exception occurred {}".format(error_log_client.report_exception()))

这里我想异步执行CF2和CF3。对于任何建议和解决方案,提前感谢您。

EN

回答 2

Stack Overflow用户

发布于 2019-11-04 23:51:38

执行异步调用的最佳服务是PubSub。为此,您必须:

  • 创建PubSub topic
  • 在之前创建的topic上使用PubSub事件触发部署CF2
  • 在之前创建的topic上使用PubSub事件触发部署CF3
  • Function CF1创建一条带参数或不带参数的PubSub消息并将其发布
  • 函数CF2和CF3与PubSub中发布的消息并行触发。他们从中提取参数,并执行

过程

如果CF2和CF3已经存在,并且是通过HTTP调用触发的,您可以在PubSub Topic上设置HTTP推送订阅。

如果CF2或CF3失败,消息将被重新发送到函数,直到收到有效的确认(2xHTTP响应)或消息TTL (默认7天)。

顺便说一句,你不是耦合的,你是可扩展的,你是并行的,错误的CF会被重试。

票数 1
EN

Stack Overflow用户

发布于 2019-11-04 23:59:19

如果您需要进行异步请求,并且您正在使用Python,您可以尝试使用aiohttpasyncio库,这里有一个here示例。此外,您还可以检查Cloud Pub/SubCloud Cloud Tasks

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58691231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档