简体   繁体   English

在 Heroku 上连续运行简单的 python 脚本

[英]Running simple python script continuously on Heroku

I have simple python script which I would like to host on Heroku and run it every 10 minutes using Heroku scheduler.我有一个简单的 python 脚本,我想在 Heroku 上托管它,并使用 Heroku 调度程序每 10 分钟运行一次。 So can someone explain me what I should type on the rake command at the scheduler and how I should change the Procfile of Heroku?那么有人可以解释我应该在调度程序的 rake 命令中输入什么以及我应该如何更改 Heroku 的 Procfile 吗?

Sure, you need to do a few things:当然,你需要做一些事情:

  1. Define a requirements.txt file in the root of your project that lists your dependencies.在项目的根目录中定义一个requirements.txt文件,其中列出了您的依赖项。 This is what Heroku will use to 'detect' you're using a Python app.这就是 Heroku 将用来“检测”您正在使用 Python 应用程序的内容。

  2. In the Heroku scheduler addon, just define the command you need to run to launch your python script.在 Heroku 调度程序插件中,只需定义您需要运行的命令来启动您的 Python 脚本。 It will likely be something like python myscript.py .它可能类似于python myscript.py

  3. Finally, you need to have some sort of web server that will listen on the proper Heroku PORT -- otherwise, Heroku will think your app isn't working and it will be in the 'crashed' state -- which isn't what you want.最后,你需要有某种网络服务器来监听正确的 Heroku 端口——否则,Heroku 会认为你的应用程序不工作,它会处于“崩溃”状态——这不是你想要的想要。 To satisfy this Heroku requirement, you can run a really simple Flask web server like this...为了满足 Heroku 的这个要求,你可以像这样运行一个非常简单的 Flask Web 服务器......

Code ( server.py ):代码( server.py ):

from os import environ
from flask import Flask

app = Flask(__name__)
app.run(environ.get('PORT'))

Then, in your Procfile , just say: web: python server.py .然后,在您的Procfile ,只需说: web: python server.py

And that should just about do it =)这应该就可以了 =)

If you use free account [unverified*] on Heroku (so you cannot install addons), instead of using "Heroku scheduler", use time.sleep(n).如果您在 Heroku 上使用免费帐户 [unverified*](因此您无法安装插件),请使用 time.sleep(n),而不是使用“Heroku 调度程序”。 You don't need Flask or any server in this case, just place script, say, inside folder Scripts (in default app/project by Heroku) and add to Procfile:在这种情况下,您不需要 Flask 或任何服务器,只需将脚本放入文件夹 Scripts(在 Heroku 的默认应用程序/项目中)并添加到 Procfile:
worker: python script.py . worker: python script.py
Of course you replace script.py with Path to your script, including name,当然,您将 script.py 替换为脚本的路径,包括名称,
ex.例如。 worker: python Scripts/my_script.py
Note: If your script uses third-party modules, say bs4 or requests, you need to install them in注意:如果你的脚本使用第三方模块,比如 bs4 或 requests,你需要将它们安装在
pipenv install MODULE_NAME or create requirements.txt and place it where manage.py, Procfile, Pipfile, (etc) are. pipenv install MODULE_NAME或创建requirements.txt并将其放在 manage.py、Procfile、Pipfile 等所在的位置。 Next place in that requirements.txt:在那个requirements.txt中的下一个地方:
requirements.txt : requirements.txt

MODULE_NAME==MODULE_VERSION

You can check them in pip freeze |您可以在 pip freeze 中查看它们 | grep MODULE_NAME grep MODULE_NAME
Finally deploy to Heroku server using git and run following command:最后使用git部署到 Heroku 服务器并运行以下命令:

heroku ps:scale worker=1

That's it!就是这样! Bot/Script is running, check it in logs: Bot/Script 正在运行,在日志中检查它:

heroku logs --tail

Source: https://github.com/michaelkrukov/heroku-python-script来源: https : //github.com/michaelkrukov/heroku-python-script


unverified* - "To help with abuse prevention, provisioning an add-on requires account verification. If your account has not been verified, you will be directed to visit the verification site."未验证* - “为了帮助防止滥用,提供附加组件需要帐户验证。如果您的帐户尚未验证,您将被引导访问验证站点。” It redirects to Credit Card info.它重定向到信用卡信息。 However you can still have Free Acc, but you will not be able to use certain options for free users, such as installing addons:但是,您仍然可以拥有 Free Acc,但您将无法为免费用户使用某些选项,例如安装插件:
https://devcenter.heroku.com/articles/getting-started-with-python#provision-add-ons https://devcenter.heroku.com/articles/getting-started-with-python#provision-add-ons

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM