1

我让模板动态重新加载,但控制器代码没有;我必须杀死并重新启动该过程。下面列出的是我运行的代码;我将它作为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()
4

1 回答 1

2

如果你想重新加载你的 Pyramid 应用程序,我建议使用 paste,而不是调用 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 文件。

于 2013-11-12T18:40:15.640 回答