7

I've built a Django project that works, even after I freeze it using Cx_Freeze and Py2exe. Now I'm trying to set up the project for distribution, which requires a real webserver. I'm going for Gunicorn (will add Nginx once it works). I managed to run the Gunicorn server properly through the command line using :

gunicorn wsgi:application

However, I need to be able to run the server from my Python script, as the server is ment to be localhost. Gunicorn used to be shipped with a command 'run_gunicorn' designed for Django, but this command is now deprecated. I tried understanding the following method :

How to use Flask-Script and Gunicorn

But I can't figure out how to make it work with Django. The following doesn't work:

from django.core.wsgi import get_wsgi_application
from gunicorn.app.base import Application

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = get_wsgi_application()
Application().run(application)

Could someone tell me how to start the gunicorn server from my Python script ?

Community
  • 1
  • 1
mrj
  • 589
  • 1
  • 7
  • 17
  • Why can't you start the server from command line? You can access the gunicorn server via localhost even through that – Aswin Murugesh Nov 26 '15 at 11:25
  • Someone else asked a related question some hours ago, maybe it can help you : [How to run a Django application automatically](http://stackoverflow.com/questions/33930640/how-to-run-my-django-application-automatically-on-production-server) – Louis Barranqueiro Nov 26 '15 at 11:42
  • Because it's a stand alone application that has to be started through an executable file. Or could it work if i simply use subprocess ? @LouisBarranqueiro: I already managed to start the Django development server automatically, but like indicated, it should not be used in production. Specifically, even if the application is localhosted, static files are not served anymore as soon as DEBUG is set to FALSE. Hence Gunicorn, which i'm trying to start automatically. – mrj Nov 26 '15 at 11:44
  • Still don't understand why do you want to write a python script to start your wsgi application with gunicorn (E.g `python your_script.py`) when `gunicorn .wsgi:application` is enough? – Louis Barranqueiro Nov 26 '15 at 11:55
  • The script is frozen using cx_freeze and is then executed with a double click by the end user. No more command line. – mrj Nov 26 '15 at 12:02
  • 3
    How about `subprocess.call(['gunicorn', 'wsgi:application'])`? – Leistungsabfall Nov 26 '15 at 12:33
  • Using a method a simple a subprocess works indeed :) Silly me, I hadn't noticed Gunicorn was UNIX only. Topic solved, can be closed ! Ty all ! – mrj Nov 27 '15 at 12:27

0 Answers0