Browse Source

added gunicorn to cherrypy

Keith Newman 10 years ago
parent
commit
f081c0272b

+ 87 - 83
frameworks/Python/cherrypy/app.py

@@ -10,101 +10,105 @@ from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy import Column
 from sqlalchemy import Column
 from sqlalchemy.types import String, Integer
 from sqlalchemy.types import String, Integer
 
 
+from saplugin import SAEnginePlugin
+from satool import SATool
+
 Base = declarative_base()
 Base = declarative_base()
 
 
 if sys.version_info[0] == 3:
 if sys.version_info[0] == 3:
-  xrange = range
+    xrange = range
 
 
 def getQueryNum(queryString):
 def getQueryNum(queryString):
-  try:
-    int(queryString)
-    return int(queryString)
-  except ValueError:
-    return 1
+    try:
+        int(queryString)
+        return int(queryString)
+    except ValueError:
+        return 1
 
 
 class Fortune(Base):
 class Fortune(Base):
-  __tablename__ = "fortune"
+    __tablename__ = "fortune"
 
 
-  id = Column(Integer, primary_key = True)
-  message = Column(String)
+    id = Column(Integer, primary_key = True)
+    message = Column(String)
 
 
 class World(Base):
 class World(Base):
-  __tablename__ = "world"
+    __tablename__ = "world"
 
 
-  id = Column(Integer, primary_key = True)
-  randomNumber = Column(Integer)
+    id = Column(Integer, primary_key = True)
+    randomNumber = Column(Integer)
 
 
-  def serialize(self):
-    return {
-      'id' : self.id,
-      'randomNumber' : self.randomNumber
-    }
+    def serialize(self):
+        return {
+            'id' : self.id,
+            'randomNumber' : self.randomNumber
+        }
 
 
 class CherryPyBenchmark(object):
 class CherryPyBenchmark(object):
 
 
-  @cherrypy.expose
-  @cherrypy.tools.json_out()
-  def json(self):
-    cherrypy.response.headers["Content-Type"] = "application/json"
-    json_message = {"message" : "Hello, world!"}
-    return json_message
-
-
-  @cherrypy.expose
-  def plaintext(self):
-    return "Hello, world!"
-
-  @cherrypy.expose
-  @cherrypy.tools.json_out()
-  def db(self):
-    cherrypy.response.headers["Content-Type"] = "application/json"
-    wid = randint(1, 10000)
-    world = cherrypy.request.db.query(World).get(wid).serialize()
-    return world
-
-  @cherrypy.expose
-  @cherrypy.tools.json_out()
-  def queries(self, queries=1):
-    num_queries = getQueryNum(queries)
-    if num_queries < 1:
-      num_queries = 1
-    if num_queries > 500:
-      num_queries = 500
-
-    rp = partial(randint, 1, 10000)
-    get = cherrypy.request.db.query(World).get
-    worlds = [get(rp()).serialize() for _ in xrange(num_queries)]
-    return worlds
-
-  @cherrypy.expose
-  @cherrypy.tools.json_out()
-  def updates(self, queries=1):
-    cherrypy.response.headers["Content-Type"] = "application/json"
-    num_queries = getQueryNum(queries)
-    if num_queries < 1:
-      num_queries = 1
-    if num_queries > 500:
-      num_queries = 500
-
-    worlds = []
-    rp = partial(randint, 1, 10000)
-    ids = [rp() for _ in xrange(num_queries)]
-    ids.sort() # To avoid deadlock
-    for id in ids:
-      world = cherrypy.request.db.query(World).get(id)
-      world.randomNumber = rp()
-      worlds.append(world.serialize())
-    return worlds
-
-if __name__ == "__main__":
-  # Register the SQLAlchemy plugin
-  from saplugin import SAEnginePlugin
-  DBDRIVER = 'mysql'
-  DBHOSTNAME = os.environ.get('DBHOST', 'localhost')  
-  DATABASE_URI = '%s://benchmarkdbuser:benchmarkdbpass@%s:3306/hello_world?charset=utf8' % (DBDRIVER, DBHOSTNAME)
-  SAEnginePlugin(cherrypy.engine, DATABASE_URI).subscribe()
-
-  # Register the SQLAlchemy tool
-  from satool import SATool
-  cherrypy.tools.db = SATool()
-  cherrypy.quickstart(CherryPyBenchmark(), '', {'/': {'tools.db.on': True}})
+    @cherrypy.expose
+    @cherrypy.tools.json_out()
+    def json(self):
+        cherrypy.response.headers["Content-Type"] = "application/json"
+        json_message = {"message" : "Hello, world!"}
+        return json_message
+
+
+    @cherrypy.expose
+    def plaintext(self):
+        return "Hello, world!"
+
+    @cherrypy.expose
+    @cherrypy.tools.json_out()
+    def db(self):
+        cherrypy.response.headers["Content-Type"] = "application/json"
+        wid = randint(1, 10000)
+        world = cherrypy.request.db.query(World).get(wid).serialize()
+        return world
+
+    @cherrypy.expose
+    @cherrypy.tools.json_out()
+    def queries(self, queries=1):
+        num_queries = getQueryNum(queries)
+        if num_queries < 1:
+            num_queries = 1
+        if num_queries > 500:
+            num_queries = 500
+
+        rp = partial(randint, 1, 10000)
+        get = cherrypy.request.db.query(World).get
+        worlds = [get(rp()).serialize() for _ in xrange(num_queries)]
+        return worlds
+
+    @cherrypy.expose
+    @cherrypy.tools.json_out()
+    def updates(self, queries=1):
+        cherrypy.response.headers["Content-Type"] = "application/json"
+        num_queries = getQueryNum(queries)
+        if num_queries < 1:
+            num_queries = 1
+        if num_queries > 500:
+            num_queries = 500
+
+        worlds = []
+        rp = partial(randint, 1, 10000)
+        ids = [rp() for _ in xrange(num_queries)]
+        ids.sort() # To avoid deadlock
+        for id in ids:
+            world = cherrypy.request.db.query(World).get(id)
+            world.randomNumber = rp()
+            worlds.append(world.serialize())
+        return worlds
+
+
+# Register the SQLAlchemy plugin
+DBDRIVER = 'mysql'
+DBHOSTNAME = os.environ.get('DBHOST', 'localhost')  
+DATABASE_URI = '%s://benchmarkdbuser:benchmarkdbpass@%s:3306/hello_world?charset=utf8' % (DBDRIVER, DBHOSTNAME)
+SAEnginePlugin(cherrypy.engine, DATABASE_URI).subscribe()
+
+# Register the SQLAlchemy tool
+
+cherrypy.tools.db = SATool()
+
+if __name__ == '__main__':
+    cherrypy.quickstart(CherryPyBenchmark(), '', {'/': {'tools.db.on': True}})

+ 2 - 1
frameworks/Python/cherrypy/bash_profile.sh

@@ -1,8 +1,9 @@
 export PY2_ROOT=$IROOT/py2
 export PY2_ROOT=$IROOT/py2
 export PY2=$PY2_ROOT/bin/python
 export PY2=$PY2_ROOT/bin/python
 export PY2_PIP=$PY2_ROOT/bin/pip
 export PY2_PIP=$PY2_ROOT/bin/pip
-
+export PY2_GUNICORN=$PY2_ROOT/bin/gunicorn
 
 
 export PY3_ROOT=$IROOT/py3
 export PY3_ROOT=$IROOT/py3
 export PY3=$PY3_ROOT/bin/python3
 export PY3=$PY3_ROOT/bin/python3
 export PY3_PIP=$PY3_ROOT/bin/pip3
 export PY3_PIP=$PY3_ROOT/bin/pip3
+export PY3_GUNICORN=$PY3_ROOT/bin/gunicorn

+ 4 - 4
frameworks/Python/cherrypy/benchmark_config

@@ -12,10 +12,10 @@
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Micro",
       "classification": "Micro",
       "database": "MySQL",
       "database": "MySQL",
-      "framework": "cherrypy",
+      "framework": "CherryPy",
       "language": "Python",
       "language": "Python",
       "orm": "Full",
       "orm": "Full",
-      "platform": "CherryPy",
+      "platform": "Meinheld",
       "webserver": "None",
       "webserver": "None",
       "os": "Linux",
       "os": "Linux",
       "database_os": "Linux",
       "database_os": "Linux",
@@ -33,10 +33,10 @@
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Micro",
       "classification": "Micro",
       "database": "MySQL",
       "database": "MySQL",
-      "framework": "cherrypy",
+      "framework": "CherryPy",
       "language": "Python",
       "language": "Python",
       "orm": "Full",
       "orm": "Full",
-      "platform": "CherryPy",
+      "platform": "Meinheld",
       "webserver": "None",
       "webserver": "None",
       "os": "Linux",
       "os": "Linux",
       "database_os": "Linux",
       "database_os": "Linux",

+ 25 - 0
frameworks/Python/cherrypy/gunicorn_conf.py

@@ -0,0 +1,25 @@
+import multiprocessing
+import os
+import sys
+
+_is_pypy = hasattr(sys, 'pypy_version_info')
+_is_travis = os.environ.get('TRAVIS') == 'true'
+
+workers = multiprocessing.cpu_count() * 3
+if _is_travis:
+    workers = 2
+
+bind = "127.0.0.1:8080"
+keepalive = 120
+errorlog = '-'
+pidfile = 'gunicorn.pid'
+
+if _is_pypy:
+    worker_class = "tornado"
+else:
+    worker_class = "meinheld.gmeinheld.MeinheldWorker"
+
+    def post_fork(server, worker):
+        # Disalbe access log
+        import meinheld.server
+        meinheld.server.set_access_logger(None)

+ 6 - 0
frameworks/Python/cherrypy/requirements.txt

@@ -1,3 +1,9 @@
 cherrypy==3.6.0
 cherrypy==3.6.0
+
 SQLAlchemy==0.9.9
 SQLAlchemy==0.9.9
 mysqlclient==1.3.6
 mysqlclient==1.3.6
+
+gunicorn==19.3.0
+meinheld==0.5.7
+
+greenlet==0.4.5

+ 1 - 1
frameworks/Python/cherrypy/setup.sh

@@ -1,3 +1,3 @@
 #!/bin/bash
 #!/bin/bash
 
 
-$PY2 app.py &
+$PY2_GUNICORN app:app -c gunicorn_conf.py --error-logfile log-gun.txt  &