简体   繁体   English

如何按特定时间表运行云中托管的简单python脚本?

[英]How can I run a simple python script hosted in the cloud on a specific schedule?

Say I have a file "main.py" and I just want it to run at 10 minute intervals, but not on my computer. 假设我有一个文件“ main.py”,我只希望它每隔10分钟运行一次,而不是在我的计算机上运行。 The only external libraries the file uses are mysql.connector and pip requests. 该文件使用的唯一外部库是mysql.connector和pip请求。

Things I've tried: 我尝试过的事情:

PythonAnywhere - free tier is too limiting (need to connect to external DB) PythonAnywhere-免费层太有限(需要连接到外部数据库)

AWS Lambda - Only supports up to Python 2.7, converted my code but still had issues AWS Lambda-仅支持多达Python 2.7,转换了我的代码,但仍然有问题

Google Cloud Platform + Heroku - can only find tutorials covering deploying applications, I think these could do what I'm looking for but I can't figure out how. Google Cloud Platform + Heroku-只能找到涵盖部署应用程序的教程,我认为这些可以满足我的需求,但我不知道如何实现。

Thanks! 谢谢!

I'd start by taking a look at this question/answer that I asked previously on unix.stackexchange - I went with an AWS redhat installation and it was free to use. 我会采取一看就我去一个AWS RedHat安装,它是免费使用-提问/回答,我以前问上unix.stackexchange。

Once you've decided on your VM, you can add SSH onto your server using any SSH client and upload your Python script. 确定虚拟机后,可以使用任何SSH客户端将SSH添加到服务器上并上传Python脚本。 A personal preference is this application. 个人喜好是应用程序。

If you need to update the Python version on the server, you can do this by installing the required Python RPMs. 如果需要更新服务器上的Python版本,可以通过安装所需的Python RPM来完成。 A quick google should return the yum [or whichever RPM management system you're using] repository for the required RPMs. 快速的google应该返回yum [或您正在使用的RPM管理系统]存储库以获取所需的RPM。

Once you've installed the version of Python that you need, I'd suggest looking into the 'crontab' which can be used to schedule jobs. 安装所需的Python版本后,建议您查看可用于安排作业的“ crontab”。 You can set a cronjob to run every 10minutes which will call your script. 您可以将cronjob设置为每10分钟运行一次,这将调用您的脚本。

See this site for more information on how to use the crontab 有关如何使用crontab的更多信息,请参见此网站。

This sounds like a perfect use case for AWS Lambda which supports Python . 这听起来像是支持Python的 AWS Lambda的完美用例。 You can invoke your Lambda on a schedule using Scheduled Events . 您可以使用Scheduled Events在时间表上调用Lambda。

I see that you tried Lambda and it didn't work out for you which is too bad as that seems like the easiest route. 我发现您尝试了Lambda,但对您来说却没有用,这太糟糕了,因为这似乎是最简单的方法。 You could also launch an EC2 instance and use userdata to schedule a cron when the instance starts. 您还可以启动EC2实例,并在实例启动时使用userdata计划cron。

Another option would be an Elastic Beanstalk worker with a cron.yml that defines your schedule. 另一个选择是具有cron.yml的Elastic Beanstalk工作者 ,该工作者定义您的计划。 Elastic Beanstalk supports Python 3.4. Elastic Beanstalk支持Python 3.4。

Update: AWS does now support Python 3.6. 更新:AWS现在支持Python 3.6。 Just select Python 3.6 from the runtime environments when configuring. 配置时,只需从运行时环境中选择Python 3.6。

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

相关问题 如何在云中安排 python 脚本? - How can I schedule python script in the cloud? 如何安排python脚本在特定时间每天运行? (Windows计划除外) - How can I Schedule python script to run every day at specific time ? (except Windows schedule) 如何按计划在后台运行我的python脚本? - How can I run my python script in the background on a schedule? 如何在云中运行 Selenium Python 脚本? - How can I run a Selenium Python Script in the Cloud? 如何在云托管服务器(Google、Amazon、Azure...)上托管 Python 服务器脚本? - How can I host a python server script, on a cloud hosted server (Google, Amazon, Azure...)? 如何在 conda env 中安排一个 python 脚本每 n 分钟运行一次? - How can I schedule a python script inside a conda env to run every n minutes? 如何运行包含来自heroku托管的php库的python脚本? - How can I run the python script which contain libraries from php hosted on heroku? 如何安排Celery Group在特定时间运行? - How can I schedule Celery Group to run at specific time? 安排简单的python脚本每天在IBM Bluemix上运行 - Schedule simple python script to run each day on IBM Bluemix 如何安排脚本每天运行 Python - How to schedule a script to run every day for Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM