简体   繁体   English

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

[英]How to run a simple python script on heroku without flask/django?

I'm trying to run a simple hello world python program on my heroku server. 我正在尝试在我的heroku服务器上运行一个简单的hello world python程序。 I'm new to heroku.I was able to successfully deploy my script to heroku. 我是heroku的新手。我能够成功地将我的脚本部署到heroku。 My python script and procfile are given below, 我的python脚本和procfile如下所示,

hi.py hi.py

print("hello world")

Procfile Procfile

web: python hi.py

I got "Hello world" as output when i ran heroku run web on my terminal.But when i try to run the app using heroku web url it shows the following error. 当我在终端上运行heroku run web时,我得到了“Hello world”作为输出。但是当我尝试使用heroku web url运行应用程序时,它显示以下错误。

Application Error An error occurred in the application and your page could not be served. 应用程序错误应用程序中发生错误,无法提供您的页面。 Please try again in a few moments. 请稍后重试。

What did i do wrong here? 我在这做错了什么? I'm newbie to heroku & its concepts, please do bare. 我是heroku及其概念的新手,请你光顾。

There are three types of dyno configurations available on Heroku: Heroku提供三种类型的dyno配置

  • Web -- receives web traffic. Web - 接收Web流量。
  • Worker -- keeps processing tasks/queues in the background. 工作者 - 在后台继续处理任务/队列。
  • One-off -- executed once. 一次性 - 执行一次。 eg: backup. 例如:备份。

If you're interested in running a script, do not care about receiving web traffic on it, and don't have a queue to process, then One-off dynos are likely what you'll want to use. 如果您对运行脚本感兴趣,不关心接收网络流量,并且没有要处理的队列,那么您可能想要使用一次性dynos This would be useful for database migrations or backups and whatnot. 这对于数据库迁移或备份以及诸如此类的东西很有用。

Minimal example below. 下面的最小例子。


Sample one-off dyno with Heroku and python AKA “hello world” 使用Heroku和python AKA“hello world”对一次性dyno进行采样

This assumes you have already created your app on Heroku and are able to use Herolu CLI from the command-line. 这假设您已经在Heroku上创建了应用程序,并且能够从命令行使用Herolu CLI。

A minimal “hello world” Python script would then look like this. 一个最小的“hello world”Python脚本就像这样。 Only 2 files required: 只需要2个文件:

  • requirements.txt Required, but can be left empty. requirements.txt必需,但可以保留为空。
  • task.py with content print("hello world") task.py with content print("hello world")

Then deploy to Heroku, eg: 然后部署到Heroku,例如:

git add .;
git commit -m "My first commit";
git push heroku master

After that, you'll be able to run your script with heroku run python task.py (and should see the long-awaited hello world in the output.) 之后,您将能够使用heroku run python task.py运行您的脚本(并且应该在输出中看到期待已久的hello world 。)

If you want to run your program at specific times, use the free Heroku Scheduler add-on. 如果要在特定时间运行程序,请使用免费的Heroku Scheduler加载项。

FYI, Procfile is optional. 仅供参考, Procfile是可选的。 If you set it to hello: python task.py then you'll be able to run your program with just heroku run hello . 如果你把它设置为hello: python task.py那么你将能够用heroku run hello运行你的程序。

(Note that leaving requirements.txt empty will trigger You must give at least one requirement to install (see "pip help install") warnings on deploy. It's just a warning though and doesn't prevent proper deployment of the program.) (请注意,将requirements.txt保留为空将触发You must give at least one requirement to install (see "pip help install")在部署You must give at least one requirement to install (see "pip help install")警告。这只是一个警告,但并不妨碍程序的正确部署。)

I disagree and state you want flask 我不同意并说你要烧瓶

main_app.py main_app.py

import flask
app = flask.Flask(__name__)

@app.route("/")
def index():
    #do whatevr here...
    return "Hello Heruko"

then change your procfile to web: gunicorn main_app:app --log-file - 然后将你的proc web: gunicorn main_app:app --log-file -更改为web: gunicorn main_app:app --log-file -

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

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