How To Host Cython Web App On Heroku?
Solution 1:
Ok, I managed to find a solution.
Let's say our project looks like this
_/cythonheroku
|-- requirements.txt
|-- Procfile
|__/app
|-- __init__.py
|-- app.pyx
Steps are as follows:
- Create a project and push it to heroku without compiling
.pyx
file with cython - You need to set your stack to
container
and install some linux packages. So, basically, you need: Remove your
Procfile
and install manifest plugin with:heroku update beta heroku plugins:install @heroku-cli/plugin-manifest heroku manifest:create
In created
heroku.yml
you need to specify packages and run command. Minimal viable manifest file will look like this:setup:config: {} build:languages:-pythonpackages:-build-essentialrun:web:'gunicorn app.app:app'
We want to install
build-essential
so linux machine on heroku side will be able to compile our cython code.run
command is a bit tricky – as far as I understand you can't run your cython app properly in heroku with something like:from app.appimport app app.run()
Heroku will put an error, that address already in use (for some reason it will run your app locally, and then try to start your app for web and you'll get an error. I don't know, I'll try to fix this somehow, but atm it's not that critical).
Change you stack to container and push:
heroku stack:set container git push heroku master
You'll see a lot of stuffs in terminal. That's ok. Heroku will try to run your app with command specified in
heroku.yml
and you'll get an error. That's because we don't have our cython file yet. Now, the problem is that compiled file is unique for every OS and, I guess, every machine (depends on CPU chipset I think). That's why we want to compile our file on heroku side not locally. To do it, you need to connect to heroku terminal, to do it just type:heroku run bash
Then you need to compile your code with simple:
cythonize -i app/app.pyx
Now, each heroku dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code, so your compiled file won't save at all. You need to download it on your machine, add to project and push to heroku again. You can do it with transfer.sh like so:
curl --upload-file ./app/<your-file> https://transfer.sh/<name-of-file>
Then you can download it with the link generated in the command line. Note: that you have to copy 2 files —
<file>.c
and<file>.cpython-36m-x86_64-linux-gnu.so
..so
file can be named differently but you got the idea.Add these two files to your project, commit and push to heroku.
That's it. Your app will be started and be available in web.
Post a Comment for "How To Host Cython Web App On Heroku?"