部署

Sanic有三个服务选项:内置的web服务器和 ASGI webservergunicorn .

Sanic自己的网络服务器是最快的选择,它可以安全地运行在互联网上。不过,将三井置于反向代理之后也是非常常见的,如中所示 Nginx部署 .

通过sanic webserver运行

在定义 sanic.Sanic ,我们可以打电话给 run 具有以下关键字参数的方法:

  • host (default `"127.0.0.1"`): Address to host the server on.

  • port (default `8000`): Port to host the server on.

  • unix (default `None`): Unix socket name to host the server on (instead of TCP).

  • debug (default `False`): Enables debug output (slows server).

  • ssl (default `None`): SSLContext for SSL encryption of worker(s).

  • sock (default `None`): Socket for the server to accept connections from.

  • workers (default `1`): Number of worker processes to spawn.

  • loop (default `None`): An asyncio-compatible event loop. If none is specified, Sanic creates its own event loop.

  • protocol (default `HttpProtocol`): Subclass of asyncio.protocol.

  • access_log (default `True`): Enables log on handling requests (significantly slows server).

app.run(host='0.0.0.0', port=1337, access_log=False)

在上面的示例中,我们决定关闭访问日志以提高性能。

工人

默认情况下,Sanic只使用一个CPU核心监听主进程。要提高效率,只需指定 run 争论。

app.run(host='0.0.0.0', port=1337, workers=4)

SANIC将自动旋转多个进程并在它们之间路由流量。我们建议尽可能多的员工使用可用的核心。

通过命令运行

如果您喜欢使用命令行参数,可以通过执行该模块来启动Sanic web服务器。例如,如果将Sanic初始化为 app 在名为 server.py ,您可以这样运行服务器:

sanic server.app --host=0.0.0.0 --port=1337 --workers=4

它也可以直接称为模块。

python -m sanic server.app --host=0.0.0.0 --port=1337 --workers=4

使用这种运行sanic的方式,不需要调用 app.run 在Python文件中。如果这样做了,请确保将其包装起来,使其仅在由解释器直接运行时执行。

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=1337, workers=4)

通过ASGi运行

Sanic也符合ASGI。这意味着您可以使用首选的ASGI web服务器来运行Sanic。ASGI的三个主要实现是 DaphneUvicornHypercorn .

按照他们的文档寻找正确的方法来运行它们,但是它应该看起来像:

daphne myapp:app
uvicorn myapp:app
hypercorn myapp:app

使用asgi时需要注意的几个事项:

一。使用Sanic webserver时,websockets将使用 websockets 包裹。在ASGI模式下,不需要这个包,因为websockets是在ASGI服务器中管理的。2。阿斯吉人 lifespan protocol <https://asgi.readthedocs.io/en/latest/specs/lifespan.html> ,仅支持两个服务器事件:启动和关闭。Sanic有四种:启动前、启动后、关闭前和关闭后。因此,在ASGI模式下,启动和关闭事件将连续运行,而不是实际围绕服务器进程的开始和结束运行(因为这现在由ASGI服务器控制)。因此,最好使用 after_server_startbefore_server_stop .

Sanic在 Trio 用:

hypercorn -k trio myapp:app

通过Gunicorn跑步

Gunicorn “Green Unicorn”是一个用于UNIX的WSGI HTTP服务器。这是一个从Ruby的Unicorn项目移植来的pre-fork工人模型。

为了使用Gunicorn运行Sanic应用程序,需要使用 sanic.worker.GunicornWorker 为了古尼孔 worker-class 论点:

gunicorn myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker

如果应用程序出现内存泄漏,可以将Gunicorn配置为在处理给定数量的请求后优雅地重新启动工作程序。这是一种帮助限制内存泄漏影响的方便方法。

Gunicorn Docs 更多信息。

其他部署注意事项

为性能禁用调试日志记录

提高性能 debug=Falseaccess_log=Falserun 争论。

app.run(host='0.0.0.0', port=1337, workers=4, debug=False, access_log=False)

通过Gunicorn运行可以设置环境变量 SANIC_ACCESS_LOG="False"

env SANIC_ACCESS_LOG="False" gunicorn myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker --log-level warning

或者直接重写app config

app.config.ACCESS_LOG = False

异步支持和共享循环

如果你愿意的话 need 与其他应用程序共享Sanic过程,特别是 loop . 但是,请注意,此方法不支持使用多个进程,并且通常不是运行应用程序的首选方式。

下面是一个不完整的示例(请参见 run_async.py 在一些更实际的例子中):

server = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(server)
loop.run_forever()

警告:使用此方法,调用 app.create_server() 将触发“before_server_start”服务器事件,但不会触发“after_server_start”、“before_server_stop”或“after_server_stop”服务器事件。

对于更高级的用例,可以使用AsyncioServer对象触发这些事件,该对象通过等待服务器任务返回。

下面是一个不完整的示例(请参见 run_async_advanced.py 在一些更完整的例子中):

serv_coro = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
loop = asyncio.get_event_loop()
serv_task = asyncio.ensure_future(serv_coro, loop=loop)
server = loop.run_until_complete(serv_task)
server.after_start()
try:
    loop.run_forever()
except KeyboardInterrupt as e:
    loop.stop()
finally:
    server.before_stop()

    # Wait for server to close
    close_task = server.close()
    loop.run_until_complete(close_task)

    # Complete all tasks on the loop
    for connection in server.connections:
        connection.close_if_idle()
    server.after_stop()