django 短链接改成长连接
1 from django.conf import settings 2 from django.core.wsgi import get_wsgi_application 3 from gunicorn.app.base import Application 4 # gunicorn 一个Python WSGI UNIX的HTTP服务器,按我的理解,它的作用可能就是用来代替django自带server的。 5 # 有了它就不用自带的runserver了,自带的server只能单线程运行,而这个能并发多线程。 6 7 class Server(Application): 8 """ 9 服务器实现类 10 """ 11 12 def __init__(self, ready_hook=None, fork_hook=None): 13 """ 14 初始化 15 """ 16 17 # 服务配置 18 self.config = dict( 19 worker_class="gthread", 20 worker_connections=5000, 21 backlog=2048, 22 threads=1, 23 daemon=True, 24 keyfile=settings.SERVER_SSL_KEY, 25 certfile=settings.SERVER_SSL_CERT, 26 27 post_worker_init=ready_hook, 28 post_fork=fork_hook, 29 30 user=settings.SERVER_USER, 31 group=settings.SERVER_GROUP, 32 bind=settings.SERVER_BIND, 33 workers=settings.SERVER_WORKERS, 34 pidfile=settings.SERVER_PID 35 ) 36 37 self.application = get_wsgi_application() 38 39 super().__init__() 40 41 def load_config(self): 42 """ 43 加载配置 44 """ 45 config = dict([(key, value) for key, value in self.config.items() 46 if key in self.cfg.settings and value is not None]) 47 48 for key, value in config.items(): 49 self.cfg.set(key.lower(), value) 50 51 def load(self): 52 """ 53 加载应用 54 """ 55 return self.application
settings.py DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", "NAME": "", "USER": "", "PASSWORD": "", "HOST": "", "PORT": "", "CONN_MAX_AGE":3600 } }
参考:https://zhuanlan.zhihu.com/p/27467118