如何在没有flask/django的heroku上运行一个简单的python脚本?

Alf*_*cis 7 python heroku

我正在尝试在我的heroku服务器上运行一个简单的hello world python程序.我是heroku的新手.我能够成功地将我的脚本部署到heroku.我的python脚本和procfile如下所示,

hi.py

print("hello world")
Run Code Online (Sandbox Code Playgroud)

Procfile

web: python hi.py
Run Code Online (Sandbox Code Playgroud)

当我heroku run web在终端上运行时,我输出了"Hello world"作为输出.但是当我尝试使用heroku web url运行应用程序时,它显示以下错误.

应用程序错误应用程序中发生错误,无法提供您的页面.请稍后重试.

我在这做错了什么?我是heroku及其概念的新手,请你光顾.

Fab*_*ert 11

三种类型的测功机配置可在Heroku:

  • Web - 接收Web流量.
  • 工作者 - 在后台继续处理任务/队列.
  • 一次性 - 执行一次.例如:备份.

如果您对运行脚本感兴趣,不关心接收网络流量,并且没有要处理的队列,那么您可能想要使用一次性dynos.这对于数据库迁移或备份以及诸如此类的东西很有用.

下面的最小例子.


使用Heroku和python AKA"hello world"对一次性dyno进行采样

这假设您已经在Heroku上创建了应用程序,并且能够从命令行使用Herolu CLI.

一个最小的"hello world"Python脚本就像这样.只需要2个文件:

  • requirements.txt 必需,但可以留空.
  • task.py 内容 print("hello world")

然后部署到Heroku,例如:

git add .;
git commit -m "My first commit";
git push heroku master
Run Code Online (Sandbox Code Playgroud)

之后,您将能够运行您的脚本heroku run python task.py(并且应该hello world在输出中看到期待已久的.)

如果要在特定时间运行程序,请使用免费的Heroku Scheduler加载项.

仅供参考,Procfile是可选的.如果你把它设置为hello: python task.py那么你将能够运行你的程序heroku run hello.

(请注意,保留requirements.txt空将You must give at least one requirement to install (see "pip help install")在部署时触发警告.虽然这只是一个警告,但不会阻止程序的正确部署.)


Jor*_*ley 6

我不同意并说你要烧瓶

main_app.py

import flask
app = flask.Flask(__name__)

@app.route("/")
def index():
    #do whatevr here...
    return "Hello Heruko"
Run Code Online (Sandbox Code Playgroud)

然后将您的procfile更改为 web: gunicorn main_app:app --log-file -