启动

当你导致 Pyramid 要在控制台窗口中启动应用程序,您将在控制台上看到类似的内容:

$VENV/bin/pserve development.ini
Starting server in PID 16305.
Serving on http://localhost:6543
Serving on http://localhost:6543

本章解释在键入后按键盘上的“回车”键之间会发生什么。 pserve development.ini 时间和线路 Serving on http://localhost:6543 输出到控制台。

启动过程

最简单和最好的文档化方式 Pyramid 应用程序将使用 pserve 对…的命令 PasteDeploy .ini 文件。这使用了 .ini 用于推断设置并启动服务器侦听端口的文件。为了讨论的目的,我们假定您正在使用此命令运行 Pyramid 应用。

以下是按时间顺序排列的高级概述,介绍当您按 return 运行后 pserve development.ini .

  1. 这个 pserve 命令在shell下用参数调用 development.ini . 因此,金字塔认识到它是要开始运行并使用包含在 development.ini 文件。

  2. pserve 通过 development.ini 到的路径 plaster 它找到一个可识别 ini 格式。

  3. plaster 找到 plaster_pastedeploy 绑定的库 PasteDeploy 并返回能够理解格式的分析器。

  4. 这个 PasteDeploy 查找名为 [app:main][pipeline:main][composite:main].ini 文件。本节配置 WSGI 为应用程序提供服务的Web服务器。正因为如此, ini 应用程序的节,可以是许多应用程序的源 settings .

    如果您使用的是简单的应用程序(例如, [app:main] )应用程序的 paste.app_factory entry point 将在上命名 use= 段配置中的行。如果不是简单的应用程序,而是使用wsgi pipeline (例如,A [pipeline:main] 在“last”元素上命名的应用程序将引用 Pyramid 应用。如果不是简单的应用程序或管道,而是使用“复合”(例如, [composite:main] ,请参阅该特定组合的文档以了解如何使其引用您的 Pyramid 应用。在大多数情况下,从cookiecutter构建的金字塔应用程序将有一个 [app:main] 其中的一部分,这将是申请书送达。

  5. 框架可以找到所有 logging 中的相关配置 .ini 文件并使用它为应用程序配置python标准库日志记录系统。见 日志记录配置 更多信息。

  6. 应用程序的入口点,通常是 above mentioned use= 行,是应用程序的 constructor . 它在中传递键/值参数 the application's .ini section . 构造函数应返回 router 实例,它是 WSGI 应用。

    为了 Pyramid 应用程序,构造函数是一个名为 main__init__.py 文件内 package 您的应用程序所在的位置。如果此函数成功,它将返回 Pyramid router 实例。下面是一个例子的内容 __init__.py 模块:

     1from pyramid.config import Configurator
     2
     3
     4def main(global_config, **settings):
     5    """ This function returns a Pyramid WSGI application.
     6    """
     7    with Configurator(settings=settings) as config:
     8        config.include('pyramid_jinja2')
     9        config.include('.routes')
    10        config.scan()
    11    return config.make_wsgi_app()
    

    注意,构造函数函数接受 global_config 参数,它是在 [DEFAULT] AN段 .ini 文件(如果 [DEFAULT] 是存在的)。它还接受 **settings 参数,它收集另一组任意的键/值对。此函数在中接收的任意键/值对 **settings 将由中存在的所有键/值对组成 [app:main] 节(除 use= 设置)运行时调用此函数时 pserve .

    我们生成的 development.ini 文件如下:

     1###
     2# app configuration
     3# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
     4###
     5
     6[app:main]
     7use = egg:myproject
     8
     9pyramid.reload_templates = true
    10pyramid.debug_authorization = false
    11pyramid.debug_notfound = false
    12pyramid.debug_routematch = false
    13pyramid.default_locale_name = en
    14pyramid.includes =
    15    pyramid_debugtoolbar
    16
    17# By default, the toolbar only appears for clients from IP addresses
    18# '127.0.0.1' and '::1'.
    19# debugtoolbar.hosts = 127.0.0.1 ::1
    20
    21###
    22# wsgi server configuration
    23###
    24
    25[server:main]
    26use = egg:waitress#main
    27listen = localhost:6543
    28
    29###
    30# logging configuration
    31# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
    32###
    33
    34[loggers]
    35keys = root, myproject
    36
    37[handlers]
    38keys = console
    39
    40[formatters]
    41keys = generic
    42
    43[logger_root]
    44level = INFO
    45handlers = console
    46
    47[logger_myproject]
    48level = DEBUG
    49handlers =
    50qualname = myproject
    51
    52[handler_console]
    53class = StreamHandler
    54args = (sys.stderr,)
    55level = NOTSET
    56formatter = generic
    57
    58[formatter_generic]
    59format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
    

    在这种情况下, myproject.__init__:main 入口点URI引用的函数 egg:myproject (见 development.ini 有关入口点uri的更多信息,以及它们如何与可调用文件相关)将接收键/值对 {{pyramid.reload_templates = true, pyramid.debug_authorization = false, pyramid.debug_notfound = false, pyramid.debug_routematch = false, pyramid.default_locale_name = en, and pyramid.includes = pyramid_debugtoolbar}} . 见 环境变量和 .ini 文件设置 为了这些键的意义。

  7. 这个 main 函数开始于 configurator . 通过 **settings 克瓦格被传给 Configurator ITS中的构造函数 settings 参数。新的配置程序保存应用程序的 settings 并且能够 commit 任何 configuration declaration s设置包含。

    这个 settings 字典包含 [app:main] .ini文件的节,除了 use 选项(在PasteDeploy内部),例如 pyramid.reload_templatespyramid.debug_authorization 等等 available for use 在您的代码中。

  8. 这个 main 然后函数对类的实例调用各种方法 Configurator 在上一步中创建。调用这些方法的目的是填充 application registry ,表示 Pyramid 与应用程序相关的配置。

  9. 这个 make_wsgi_app() 方法被调用。结果是 router 实例。路由器与 application registry 由先前由其他方法填充的配置器所暗示,这些方法运行在配置器上。路由器是一个wsgi应用程序。

  10. ApplicationCreated 事件已发出(请参见 使用事件 有关事件的详细信息)。

  11. 假设没有错误, main 中的函数 myproject 返回由创建的路由器实例 pyramid.config.Configurator.make_wsgi_app() 回到 pserve . 到目前为止 pserve 值得关注的是,它只是“另一个WSGi应用程序”。

  12. pserve 启动WSGI 服务器 在中定义 [server:main] 部分。在我们的情况下,这是服务员 (use = egg:waitress#main ,它将监听端口6543上的所有接口,包括IPv4和IPv6。 (listen = localhost:6543 )服务器代码本身就是打印的 Serving on http://localhost:6543 . 服务器为应用程序提供服务,应用程序正在运行,等待接收请求。

参见

日志配置在 登录 章。在那里 请求使用Paste的Translogger进行日志记录 ,您还将找到如何配置的示例 middleware 向应用程序添加预打包功能。

部署设置

请注意,作为 **settingsConfigurator 构造函数可用于 Pyramid view callable 代码作为 request.registry.settings . 您可以从视图代码中创建稍后要访问的对象,并将它们作为 settings . 它们将出现在 request.registry.settings 应用程序运行时的字典。