Deploying Python on Heroku - LabMazurokCom/Blockchain GitHub Wiki
Download Heroku CLI and log in to Heroku with the command
heroku login
You'll be asked to enter your email and password.
If you deploy a new app then create it with Heroku CLI:
heroku create your_app_name
If you run
heroku create
then Heroku will generate name automatically. It will be something like mystic-wind-83 or thawing-inlet-61413.
Anyway you can later rename your app.
Install git following these instructions.
If the app is already deployed to Heroku you can get its current code:
heroku git:clone -a application_name
This command will clone the app folder to your current working directory. Of course you have to log in to Heroku before using this.
Python application needs a couple of files which specify its dependencies and the way Heroku must run it.
There must be requirements.txt file in the root directory of your app. This file should contain full list of non-stdlib packages required to run your app. You can specify minimum or exact version of any package if you need. Here's an example:
aiohttp==3.1.3
async-timeout>=2.0
pyrebase
You will also need a special file called Procfile. Here's the description from Heroku docs:
It should be named Procfile exactly, and not anything else. For example, Procfile.txt is not valid. The file should be a simple text file. The file must be placed in the root directory of your application. It will not function if placed in a subdirectory.
Each line of Procfile has to contain exactly one process type in the wollowing format:
<process type>: <command>
process type is an alphanumeric string, a name for your command (clock, release, urgentworker, web, worker) etc.
command is a command line to launch the process.
The web process type is special as it’s the only process type that will receive HTTP traffic from Heroku’s routers.
The release process type is special and is used to run a release phase command.
Other process types can be named arbitrarily.
Here's an example:
worker: python logger.py -l 50 btc_usd
Probably you will also want to add .gitignore file so that git will ignore your virtual environment, temporary files etc. This file could look like this:
Include/
Lib/
Scripts/
pip-selfcheck.json
pyvenv.cfg
test.py
Initialize a Git repository (this has to be done only once):
git init
Add a remote to your local repository:
heroku git:remote -a your_application_name
Deploy your app to Heroku:
git add .
git commit -m "Your commit text"
git push heroku master
Run your app with one-off dyno or scale the process types mentioned in Procfile.
Here's an example of running "arbitrage-logger" app in background with one-off dyno:
heroku run:detached --app arbitrage-logger python logger.py -l 50 btc_usd
Here's an example of scaling a working process type:
heroku ps:scale worker=1
There are certain limits for scaling dynos:
The free and hobby dyno types only support a maximum of one dyno running per process type. Additionally, applications using a free dyno type are limited to a maximum of two concurrent running dynos.
By default, all applications are limited to 100 dynos. Additionally, a process type can’t be scaled to more than 10 dynos for performance dynos.
If the account is not verified, then it cannot have more than 3 one-off dynos of type free or hobby running concurrently. Accounts that aren’t verified can’t create one-off standard or one-off performance dynos.
For verified accounts, no more than 5 one-off dynos of each performance dyno size can run concurrently (e.g., 5 performance-m and 5 performance-l).
Standard-1x and standard-2x have a limit of 50 concurrent one-off dynos.
Remember also that free dynos will go to sleep after 30 minutes of inactivity. If an app running on a free dyno has to run continuously then sleeping shoul be handled somehow.