简体   繁体   English

如何在不手动启动它们的情况下在Google Cloud项目或AWS实例上运行python脚本?

[英]How can I run a python script on Google Cloud project or AWS instances without manually firing them up?

I have some python scripts that require a good machine. 我有一些需要一台好的机器的python脚本。 Instead of starting instances manually on GCP or AWS, then making sure all python libraries are installed, can I do it through python for example so that the instance is on only for the time needed to run the script? 无需确保在GCP或AWS上手动启动实例,而是确保安装了所有python库,我可以通过python进行操作,以便仅在运行脚本所需的时间内打开实例吗?

如果您使用的是AWS,则可以为脚本创建Lambda函数,然后通过Lambda在计时器上设置这些函数,或使用Cloudwatch触发它们。

In both AWS and Google Cloud, you can do just about anything via a programming language including Python. 在AWS和Google Cloud中,您都可以通过包括Python在内的编程语言执行几乎所有操作。

Last year, AWS announced EC2 Pause and Resume. 去年,AWS宣布了EC2暂停和恢复。 This feature allows you to setup and configure an EC2 instance and when you are finished with your data processing, put the instance to sleep. 此功能使您可以设置和配置EC2实例,并在完成数据处理后将实例置于睡眠状态。 You then just pay for storage and IP address costs. 然后,您只需支付存储和IP地址费用。

New – Hibernate Your EC2 Instances 新增–休眠EC2实例

Google has also announced alpha features for pausing Compute Engine instances, but this feature is not generally available today - you must apply to use this feature. Google还宣布了用于暂停Compute Engine实例的alpha功能,但是该功能今天通常不可用-您必须申请使用此功能。

Another option supported by both AWS and Google today is instance templates. 今天,AWS和Google都支持的另一个选项是实例模板。 This allows you to create a template with all the options that you want, such as installing packages on startup. 这使您可以使用所需的所有选项来创建模板,例如在启动时安装软件包。 You can then launch a new custom instance from the console, CLI or with your favorite programming language. 然后,您可以从控制台,CLI或使用您喜欢的编程语言启动新的自定义实例。 When your task is complete you can then stop or terminate the instance. 任务完成后,您可以停止或终止实例。

Of course there is also the standard method. 当然也有标准方法。 Launch an instance, configure as required and then stop the instance. 启动实例,根据需要进行配置,然后停止实例。 When you need processing power, start the instance, data process and then stop again. 需要处理能力时,请启动实例,数据处理,然后再次停止。 The difference between this method and pausing an instance is the total time to start an instance is faster with resume. 此方法与暂停实例之间的区别在于,使用简历启动实例的总时间更快。 Sort of like your laptop. 有点像您的笔记本电脑。 Close the lid and the laptop goes to sleep. 合上盖子,笔记本电脑进入睡眠状态。 Open the lid and you have almost instant-on. 打开盖子,您几乎可以立即开机。

If you are fortunate enough to have a running Kubernetes cluster, you can do everything with a container and launch the container via CLI. 如果您有幸拥有一个正在运行的Kubernetes集群,则可以使用容器执行所有操作并通过CLI启动该容器。 The container will automatically stop once the container finishes its task. 容器完成其任务后,它将自动停止。

You can have a script that invokes AWS CLI to start up an instance, connect to it and run the script via SSH and then terminate the instance. 您可以具有一个脚本,该脚本调用AWS CLI来启动实例,连接到该实例并通过SSH运行该脚本,然后终止该实例。 See AWS CLI documentation here - https://docs.aws.amazon.com/cli/latest/userguide/cli-ec2-launch.html . 请参阅AWS CLI文档-https: //docs.aws.amazon.com/cli/latest/userguide/cli-ec2-launch.html

An alternative to @bspeagle's suggestion of AWS Lambda is GCP's Cloud Functions. @bspeagle关于AWS Lambda的建议的替代方法是GCP的云功能。

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

相关问题 如何在 AWS Lambda 中手动运行 python 脚本? - How can I run python script manually in AWS Lambda? 如何在Google Cloud中的文件上运行Python脚本 - How to run Python script on files in Google Cloud 如何在Google Cloud Storage上运行Python脚本? - How to run Python script on Google Cloud storage? 如何在云中运行 Selenium Python 脚本? - How can I run a Selenium Python Script in the Cloud? 如何在 Google 云平台 gcp 上托管 python 脚本? - How can I host a python script on Google cloud platform gcp? 如何在AWS Lambda 24/7上运行python脚本 - How can I run my python script on AWS lambda 24/7 Python UnitTest-如何访问subTests消息而不必手动编写它们? - Python UnitTest - How can I access subTests messages without having to write them manually? 如何制作一个 cron.yaml 文件以每小时运行一次 python 脚本,用于谷歌云应用引擎 - How can I make a cron.yaml file to run python script every hour for google cloud app engine 如何设置在Python脚本中运行的时间 - How can I set up the time to run in Python Script 如何在由谷歌云存储完成事件触发的 python 脚本中运行 bash 脚本? - How do I run a bash script inside python script triggered by google cloud storage finalize event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM