简体   繁体   English

如何在 GCP 中每天运行 Python 脚本?

[英]How to run a Python script every day in GCP?

I have a simple script, it retrieves data from an API and loads it into BigQuery.我有一个简单的脚本,它从 API 检索数据并将其加载到 BigQuery 中。 I was using Cloud Functions and it was running smoothly, however there came a time that reached the 9-minute run time limit.我使用的是 Cloud Functions,它运行平稳,但是有一次达到了 9 分钟的运行时间限制。

What is the best way to do this in GCP, taking the time the script needs?花脚本需要的时间在 GCP 中执行此操作的最佳方法是什么? I was thinking of creating another Cloud Function that daily starts a preemtible VM, the VM executes the script and in the end turns itself off.我正在考虑创建另一个 Cloud Function,它每天启动一个抢占式 VM,VM 执行脚本并最终自行关闭。 To keep the price low, the VM would always shut down at the end of the data load.为了保持低价,VM 总是在数据加载结束时关闭。 It would start the next day at the selected time.它将在第二天的选定时间开始。

I don't know where to start to do this, but I was wondering if that would be the best way.我不知道从哪里开始这样做,但我想知道这是否是最好的方法。

Cloud functions aren't really suited to batch jobs that may be longer running than 10 minutes.云功能并不真正适合运行时间可能超过 10 分钟的批处理作业。 I'd suggest running your job using a Compute Engine VM and scheduling it with a combination of Cloud functions / Cloud scheduler.我建议使用 Compute Engine 虚拟机运行您的工作,并结合云功能/云调度程序来调度它。

Here's a rough outline:这是一个粗略的大纲:

  1. Set up a containerized Compute Engine VM.设置一个容器化的 Compute Engine 虚拟机。
  2. Create a Cloud function to start the VM on a pub-sub trigger.创建云 function 以在发布-订阅触发器上启动 VM。
import googleapiclient.discovery

def start_job(event, context):
    """Triggered from a message on a Cloud Pub/Sub topic.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    compute = googleapiclient.discovery.build('compute', 'v1')
    compute.instances().insert(
        project='project_id',
        zone='us-east1-b',
        body=vm_config).execute()
  1. Create a Cloud Scheduler to trigger the pub-sub according to your schedule.创建一个 Cloud Scheduler 以根据您的计划触发 pub-sub。

This lets you avoid the cost of an always-on VM.这使您可以避免永远在线 VM 的成本。 See this blog post for more detail.有关更多详细信息,请参阅此博客文章

Could this work?这能行吗?

import schedule
import time

def run_daily():
    do something
    do something else        


schedule.every().day.at("08:20:30").do(run_daily) # HH MM SS

while True:
    schedule.run_pending()
    time.sleep(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何安排脚本每天运行 Python - How to schedule a script to run every day for Python Python:如何自动化脚本在特定时间每天运行? - Python: How to automate script to run every day at specific time? 如何在 GCP 的 Cron 中运行 Python 脚本? - How to run Python script in Cron in GCP? 如何在任何地方使用像python这样的免费在线服务器每天同时运行python脚本? - How to use free online servers like python anywhere to run python script at the same time every day? time.sleep 是每天运行 python 脚本的好方法吗? - Is time.sleep a good way to run a python script every day? 如何安排生产 python 脚本以触发 function 在每天的指定时间运行 - How to schedule a production python script to trigger a function to run at a specified time every Day 如何安排python脚本在特定时间每天运行? (Windows计划除外) - How can I Schedule python script to run every day at specific time ? (except Windows schedule) 如何在整个一周(白天和黑夜)每 5 分钟轻松且廉价地运行一个简单的 python 脚本? - How to easily and cheaply run a simply python script every 5 mins for a whole week - day and night? 如何连续运行python脚本一天 - how to run a python script constantly for a day 如何每天在特定时间运行python函数 - How to run a python function every day at specific time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM