sebastianspiegel
sebastianspiegel

Reputation: 455

Can I use Heroku as a Python server?

My web host does not have python and I am trying to build a machine learning application. I know that heroku lets you use python. I was wondering if I could use heroku as a python server? As in I would let heroku do all of the python processing for me and use my regular domain for everything else.

Upvotes: 1

Views: 2069

Answers (3)

soysoy
soysoy

Reputation: 319

Have you done your Python Server on Heroku by using twisted?

I don't know if this can help you.

I see the doc 'Getting Started on Heroku with Python' is about the Django.

It is sure that Heroku can use Twisted from docs

Pure Python applications, such as headless processes and evented web frameworks like Twisted, are fully supported.

django-twisted-server has twisted in django but it isn't on Heroku.

Upvotes: -1

Ian Price
Ian Price

Reputation: 7616

Yes, and it may be a pain at first but once it is set I would say Heroku is the easiest platform to continually deploy to. However, it is not intuitive - don't try and just 'take a stab' at it; follow a tutorial and try and understand why Heroku works the way it does.

Following the docs is a good bet; Heroku has great documentation for the most part.

Here's the generalized workflow for deploying to Heroku:

  1. Locally, create your project and use virtualenv to install/manage libraries.
  2. Initialize a git repository in the base dir for your Python project; create a heroku remote (heroku create)
  3. Create a procfile for Heroku to use when starting gunicorn (or see the options for using waitress/etc); this is used by Heroku to start your process
  4. cd to your base dir; freeze your virtualenv (pip freeze > requirements.txt) and add/commit requirements.txt. This tells Heroku what packages need to be installed, a requirement for your deployment to work. If you are trying to run a Python project and there are required packages missing, the app will be unable to start and Heroku will display an Internal Server Error.
  5. Whenever changes are made, git commit your changes and git push heroku master to push all commits to Heroku. This will cause Heroku to restart the server application with your updated deployment. If there's a failure, you can use heroku rollback to just return to your last deployment.

In reality, it's not a pain in the ass, just particular. Knowing the rules of Heroku, you are able to manage your deployment with command-line git commands with ease.

One caveat - If deploying Django, Flask applications etc there are peculiarities to account for; specifically, non-project files (including assets) should NOT be stored on Heroku as Heroku periodically restarts your 'dyno' (server instance(s)), loading the whole project from the latest push to Heroku. With Django and Flask, this typically means serving assets/static/media files from an Amazon S3 bucket.

That being said, if you use virtualenv properly, provision your databases, and follow Heroku practices for serving files and commiting updates, it is (imho) the absolute best platform out there for ease of use, reliable uptime, and well-oiled rolling deployments.

One last tip - if you are creating a Django app, I'd suggest starting your project out of this boilerplate. I have a custom one I use for new projects and can start and publish a project in minutes.

Upvotes: 2

TheGreatContini
TheGreatContini

Reputation: 6629

Yes, you can use Heroku as a python server. I put a Python Flask server on Heroku but it was a pain: Heroku seemed to have some difficulties, and there were lots of conflicting advice on getting around those. I eventually got it working, can't remember what web page had the ultimate answer but you might look at this one: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xviii-deployment-on-the-heroku-cloud

Upvotes: 0

Related Questions