Merge branch 'feature/gunicorn'

This commit is contained in:
Martin Zimmermann 2013-11-25 16:57:45 +01:00
commit a045c963bd
2 changed files with 36 additions and 36 deletions

View File

@ -33,10 +33,11 @@ dist = pkg_resources.get_distribution("isso")
try: try:
import uwsgi import uwsgi
except ImportError: except ImportError:
uwsgi = None
try: try:
import gevent.monkey; gevent.monkey.patch_all() import gevent.monkey; gevent.monkey.patch_all()
except ImportError: except ImportError:
pass gevent = None
import sys import sys
import os import os
@ -46,6 +47,7 @@ import tempfile
from os.path import dirname, join from os.path import dirname, join
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import partial, reduce
from itsdangerous import URLSafeTimedSerializer from itsdangerous import URLSafeTimedSerializer
@ -56,12 +58,13 @@ from werkzeug.wsgi import SharedDataMiddleware
from werkzeug.local import Local, LocalManager from werkzeug.local import Local, LocalManager
from werkzeug.serving import run_simple from werkzeug.serving import run_simple
from werkzeug.contrib.fixers import ProxyFix from werkzeug.contrib.fixers import ProxyFix
from werkzeug.contrib.profiler import ProfilerMiddleware
local = Local() local = Local()
local_manager = LocalManager([local]) local_manager = LocalManager([local])
from isso import db, migrate, wsgi, ext, views from isso import db, migrate, wsgi, ext, views
from isso.core import ThreadedMixin, uWSGIMixin, Config from isso.core import ThreadedMixin, ProcessMixin, uWSGIMixin, Config
from isso.utils import parse, http, JSONRequest, origin from isso.utils import parse, http, JSONRequest, origin
from isso.views import comments from isso.views import comments
@ -139,13 +142,14 @@ class Isso(object):
def make_app(conf=None): def make_app(conf=None):
try: if uwsgi:
import uwsgi class App(Isso, uWSGIMixin):
except ImportError: pass
elif gevent or sys.argv[0].endswith("isso"):
class App(Isso, ThreadedMixin): class App(Isso, ThreadedMixin):
pass pass
else: else:
class App(Isso, uWSGIMixin): class App(Isso, ProcessMixin):
pass pass
isso = App(conf) isso = App(conf)
@ -158,23 +162,22 @@ def make_app(conf=None):
else: else:
logger.warn("unable to connect to HTTP server") logger.warn("unable to connect to HTTP server")
wrapper = [local_manager.make_middleware]
if isso.conf.getboolean("server", "profile"): if isso.conf.getboolean("server", "profile"):
from werkzeug.contrib.profiler import ProfilerMiddleware as Profiler wrapper.append(partial(ProfilerMiddleware,
ProfilerMiddleware = lambda app: Profiler(app, sort_by=("cumtime", ), restrictions=("isso/(?!lib)", 10)) sort_by=("cumtime", ), restrictions=("isso/(?!lib)", 10)))
else:
ProfilerMiddleware = lambda app: app
app = ProxyFix( wrapper.append(partial(SharedDataMiddleware, exports={
wsgi.SubURI(
wsgi.CORSMiddleware(
SharedDataMiddleware(
ProfilerMiddleware(
local_manager.make_middleware(isso)), {
'/js': join(dirname(__file__), 'js/'), '/js': join(dirname(__file__), 'js/'),
'/css': join(dirname(__file__), 'css/')}), '/css': join(dirname(__file__), 'css/')}))
origin(isso.conf.getiter("general", "host")))))
return app wrapper.append(partial(wsgi.CORSMiddleware,
origin=origin(isso.conf.getiter("general", "host"))))
wrapper.extend([wsgi.SubURI, ProxyFix])
return reduce(lambda x, f: f(x), wrapper, isso)
def main(): def main():
@ -221,9 +224,5 @@ def main():
raise raise
wsgi.SocketHTTPServer(sock, make_app(conf)).serve_forever() wsgi.SocketHTTPServer(sock, make_app(conf)).serve_forever()
try:
import uwsgi application = make_app(Config.load(os.environ.get('ISSO_SETTINGS')))
except ImportError:
pass
else:
application = make_app(Config.load(os.environ.get('ISSO_SETTINGS')))

View File

@ -5,9 +5,10 @@ from __future__ import print_function
import io import io
import os import os
import time import time
import logging
import binascii import binascii
import threading import threading
import logging import multiprocessing
from configparser import ConfigParser from configparser import ConfigParser
@ -213,6 +214,14 @@ class ThreadedMixin(Mixin):
time.sleep(delta) time.sleep(delta)
class ProcessMixin(ThreadedMixin):
def __init__(self, conf):
super(ProcessMixin, self).__init__(conf)
self.lock = multiprocessing.Lock()
class uWSGICache(object): class uWSGICache(object):
"""Uses uWSGI Caching Framework. INI configuration: """Uses uWSGI Caching Framework. INI configuration:
@ -241,15 +250,7 @@ class uWSGIMixin(Mixin):
super(uWSGIMixin, self).__init__(conf) super(uWSGIMixin, self).__init__(conf)
class Lock(): self.lock = multiprocessing.Lock()
def __enter__(self):
uwsgi.lock()
def __exit__(self, exc_type, exc_val, exc_tb):
uwsgi.unlock()
self.lock = Lock()
self.cache = uWSGICache self.cache = uWSGICache
timedelta = conf.getint("moderation", "purge-after") timedelta = conf.getint("moderation", "purge-after")