Created
October 26, 2019 07:41
-
-
Save multiversecoder/3aed5d73fb5eaf1c4ba71099873db409 to your computer and use it in GitHub Desktop.
Gunicorn Adapter for Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import multiprocessing | |
from django.core.wsgi import get_wsgi_application | |
from typing import Callable | |
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings' # path to your settings module | |
def workers() -> int: | |
return (multiprocessing.cpu_count() * 2) + 1 | |
class GunicornServer: | |
def __init__(self, host: str, port: int, **options): | |
self.host = host | |
self.port = port | |
self.options = options | |
def run(self, handler: Callable): | |
from gunicorn.app.base import Application | |
config = { | |
'bind': "%s:%d" % (self.host, int(self.port)), | |
"workers": workers() | |
} | |
config.update(self.options) | |
class GunicornApplication(Application): | |
def init(self, parser, opts, args): | |
return config | |
def load(self): | |
return handler | |
GunicornApplication().run() | |
if __name__ == "__main__": | |
GunicornServer("127.0.0.1", 8888).run(get_wsgi_application()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment