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

后端 未结 2 1052
既然无缘
既然无缘 2020-12-31 14:03

I\'m trying to run a simple hello world python program on my heroku server. I\'m new to heroku.I was able to successfully deploy my script to heroku. My python script and pr

相关标签:
2条回答
  • 2020-12-31 14:24

    There are three types of dyno configurations available on Heroku:

    • Web -- receives web traffic.
    • Worker -- keeps processing tasks/queues in the background.
    • One-off -- executed once. e.g.: 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. 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”

    This assumes you have already created your app on Heroku and are able to use Herolu CLI from the command-line.

    A minimal “hello world” Python script would then look like this. Only 2 files required:

    • requirements.txt Required, but can be left empty.
    • task.py with content print("hello world")

    Then deploy to Heroku, e.g.:

    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.)

    If you want to run your program at specific times, use the free Heroku Scheduler add-on.

    FYI, Procfile is optional. If you set it to hello: python task.py then you'll be able to run your program with just 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.)

    0 讨论(0)
  • 2020-12-31 14:36

    I disagree and state you want flask

    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 -

    0 讨论(0)
提交回复
热议问题