一个用 C 编写的非常快的 Python 2 + 3 WSGI 服务器。
项目描述
一个用于 CPython 2 和 CPython 3 的极其快速、超轻量级的WSGI服务器,使用 Marc Lehmann 的高性能libev事件循环和 Ryan Dahl 的http-parser用 C 语言编写。
为什么很酷
bjoern 是目前最快、最小和最轻量级的WSGI 服务器,具有
~ 1000 行 C 代码
内存占用 ~ 600KB
Python 2 和 Python 3 支持(感谢@yanghao!)
单线程且没有协程或其他废话
可以绑定到 TCP主机:端口地址和 Unix 套接字(感谢 @k3d3!)
在 HTTP/1.0 和 1.1 中都支持完整的持久连接(“ keep-alive ”),包括对 HTTP/1.1 分块响应的支持
安装
点安装 bjoern。有关详细信息,请参阅维基。
用法
烧瓶示例
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello, World!"
if __name__ == "__main__":
import bjoern
bjoern.run(app, "127.0.0.1", 8000)
高级用法
# Bind to TCP host/port pair:
bjoern.run(wsgi_application, host, port)
# TCP host/port pair, enabling SO_REUSEPORT if available.
bjoern.run(wsgi_application, host, port, reuse_port=True)
# Bind to Unix socket:
bjoern.run(wsgi_application, 'unix:/path/to/socket')
# Bind to abstract Unix socket: (Linux only)
bjoern.run(wsgi_application, 'unix:@socket_name')
# Enable statsd metrics. See instrumentation.md for details.
bjoern.run(wsgi_application, host, port, statsd=...)
或者,主循环可以单独运行:
bjoern.listen(wsgi_application, host, port)
bjoern.run()
# With metrics. See instrumentation.md for details.
bjoern.listen(wsgi_application, host, port)
bjoern.run(statsd=...)
你也可以简单地传递一个 Python socket(-like) 对象。请注意,在这种情况下,您负责初始化和清理套接字。
bjoern.server_run(socket_object, wsgi_application)
bjoern.server_run(filedescriptor_as_integer, wsgi_application)
# This needs manual compilation with `WANT_STATSD=yes`
bjoern.server_run(socket_object, wsgi_application, enable_statsd=True)