代码在金字塔中自动重新加载

时间:2013-11-12 16:06:55

标签: pyramid

我让模板动态重新加载,但控制器代码没有;我必须杀死并重启该过程。下面列出的是我运行的代码;我将其作为python app.py运行。

我错误配置了什么以及如何让代码动态重新加载?

if __name__ == '__main__':
    # configuration settings
    settings = {}
    settings['reload_all'] = True
    settings['debug_all'] = True
    settings['mako.directories'] = os.path.join(here, 'templates')
    #settings['db'] = os.path.join(here, 'tasks.db')
    # session factory
    session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet')
    # configuration setup
    config = Configurator(settings=settings, session_factory=session_factory)
    # routes setup
    #config.add_route('list', '/')
    #config.add_route('new', '/new')
    #config.add_route('close', '/close/{id}')

    #config.add_route('hello', '/hello')
    config.add_route('meta', '/m/{mid}')
    # static view setup
    config.add_static_view('static', os.path.join(here, 'static'))
    # scan for @view_config and @subscriber decorators

    connection = pymongo.Connection()
    def add_db(request):
        #db = config.registry.db[db_url.path[1:]]
        #if db_url.username and db_url.password:
            #db.authenticate(db_url.username, db_url.password)
        db = connection["bricwall_pyramid"]["bricwall"]
        return db

    #def add_fs(request):
       #return GridFS(request.db)

    config.add_request_method(add_db, 'db', reify=True)
    #config.add_request_method(add_fs, 'fs', reify=True)

    config.scan()
    # serve app
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()

1 个答案:

答案 0 :(得分:2)

如果您想重新加载Pyramid应用程序,我建议使用贴纸,而不是调用server.serve_forever()

一种简单的方法是使用金字塔的脚手架:

您可以使用Starter项目脚手架:

pcreate -s starter MyProject

或者,使用Sqlalchemy(但我看到你正在使用pymongo,所以我认为第一个是最好的选择):

pcreate -s alchemy MyProject

这将创建默认的MyProject应用程序,然后您必须安装它:

python setup.py develop

然后运行:

pserve development.ini --reload

这里有很好的解释:http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/project.html

但您的申请必须改为:

def main(global_config, **settings)

    config = Configurator(settings=settings, session_factory=session_factory)
    # routes setup
    #config.add_route('list', '/')
    #config.add_route('new', '/new')
    #config.add_route('close', '/close/{id}')

    #config.add_route('hello', '/hello')
    config.add_route('meta', '/m/{mid}')
    # static view setup
    config.add_static_view('static', os.path.join(here, 'static'))
    # scan for @view_config and @subscriber decorators

    connection = pymongo.Connection()
    def add_db(request):
        #db = config.registry.db[db_url.path[1:]]
        #if db_url.username and db_url.password:
            #db.authenticate(db_url.username, db_url.password)
        db = connection["bricwall_pyramid"]["bricwall"]
        return db

    #def add_fs(request):
       #return GridFS(request.db)

    config.add_request_method(add_db, 'db', reify=True)
    #config.add_request_method(add_fs, 'fs', reify=True)

    config.scan()
    # serve app
    app = config.make_wsgi_app()
    return app

您必须将 init 代码包装在main函数中,并将设置转到development.ini文件。