2014-11-15 45 views
1

我正在使用瓶子编写一个小型web程序并将以下源文件命名为index.py。我也在程序中使用烧杯会话库。当我使用运行代码时,一切正常。但是当我使用gunicorn -c gunicorn.conf index:app时,我收到这样的错误消息,说烧杯密钥beaker.session不存在。如何更改代码以使其在gunicorn服务器中再次运行?尝试使用python中的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) 

回答

0

我相信你并不需要的最后一块if __name...可言。 Gunicorn运行索引模块的应用程序“varibale”作为WSIG意味着它应该启动一个瓶子应用程序的实例。