1

我正在使用瓶子编写一个小型 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)
4

1 回答 1

0

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

于 2014-12-25T20:00:33.200 回答