尝试在python Bottle框架中使用gunicorn进行烧杯会话

时间:2014-11-15 13:04:08

标签: python bottle gunicorn beaker

我正在使用bottle编写一个小型Web程序并命名以下源文件index.py。我还在程序中使用了烧杯会话库。当我使用python index.py运行代码时,一切正常。但是当我使用gunicorn -c gunicorn.conf index:app时,我收到类似这样的错误消息,说烧杯密钥beaker.session不存在。如何在gunicorn服务器中更改代码以使其重新运行?

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/bottle-0.12.7-py2.7.egg/EGG-INFO/scripts/bottle.py", line 857, in _handle
    self.trigger_hook('before_request')
  File "/Library/Python/2.7/site-packages/bottle-0.12.7-py2.7.egg/EGG-INFO/scripts/bottle.py", line 640, in trigger_hook
    return [hook(*args, **kwargs) for hook in self._hooks[__name][:]]
  File "/Users/yizeng/Documents/python_projects/simple-courses/index.py", line 17, in setup_request
    request.session = request.environ['beaker.session']
KeyError: 'beaker.session'

index.py的源代码:

import os, sys, bottle
from bottle import debug, route, request, run, Bottle, static_file, hook
from apps import model, views
from beaker.middleware import SessionMiddleware


app = bottle.app()

session_options = {
        'session.type': 'file',
        'session.cookie_expires': 300,
        'session.data_dir': './data',
        'session.auto': True
    }
@hook('before_request')
def setup_request():
    request.session = request.environ['beaker.session']

@app.route('/assets/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='assets')

@app.route('/test')
def test():
    s = request.environ.get('beaker.session')
    s['test'] = s.get('test', 0) + 1
    s.save()
    return 'Test counter: %d' % s['test']

app_session = SessionMiddleware(app, session_options)
if __name__ == '__main__':
    app.run(app=app_session)

1 个答案:

答案 0 :(得分:0)

我相信你根本不需要最后一个if __name...块。 Gunicorn运行索引模块的 app &#34; varibale&#34;作为一个WSIG意味着它应该启动你的瓶子应用程序的实例。