Python中如何使用Gunicorn worker运行Sanic应用?
https://github.com/messense/sanic-gunicorn
也给官方发了一个 PR: https://github.com/channelcat/sanic/pull/545
Python中如何使用Gunicorn worker运行Sanic应用?
弱弱的问一句, Gunicorn 自带的 AiohttpWorker 不能运行 sanic?
在Sanic应用中直接使用Gunicorn作为WSGI服务器是不推荐的,因为Sanic是一个异步框架,而Gunicorn的默认worker是同步的。不过,你可以通过使用异步worker(如uvicorn.workers.UvicornWorker)来运行Sanic应用。以下是具体步骤:
首先,确保安装了必要的包:
pip install sanic gunicorn uvicorn
然后,创建一个简单的Sanic应用(例如app.py):
from sanic import Sanic
from sanic.response import text
app = Sanic("MyApp")
@app.get("/")
async def hello(request):
return text("Hello, world!")
最后,使用Gunicorn启动应用,指定Uvicorn worker:
gunicorn app:app --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
这里,app:app表示从app.py模块中导入app实例。--worker-class指定使用Uvicorn worker,它支持异步操作。--bind设置绑定的地址和端口。
总结:使用Uvicorn worker运行Sanic应用。
66666
好像 aiohttp 的速度不够快啊,不过我也没试……待验证。
根本起不来
gunicorn simple_server:app --bind localhost:8000 --worker-class gaiohttp --capture-output
[2017-03-20 20:13:34 +0800] [3226] [INFO] Starting gunicorn 19.6.0
[2017-03-20 20:13:34 +0800] [3226] [INFO] Listening at: http://127.0.0.1:8000 (3226)
[2017-03-20 20:13:34 +0800] [3226] [INFO] Using worker: gaiohttp
[2017-03-20 20:13:34 +0800] [3260] [INFO] Booting worker with pid: 3260
Application object must be callable.
[2017-03-20 20:13:34 +0800] [3260] [INFO] Worker exiting (pid: 3260)
[2017-03-20 20:13:34 +0800] [3226] [INFO] Shutting down: Master
[2017-03-20 20:13:34 +0800] [3226] [INFO] Reason: App failed to load.
也需要一些 hack ,比如类似: https://github.com/messense/sanic-gunicorn/commit/e60a683f976cd66a36f3c1ee88b92f4be190067a#diff-757823b0d65a4f88a48899c3fd4135c8R54
或者自己实现下 call
#4 actually ,即便实现了 call 也不好继续下去,因为 Sanic 并不兼容 WSGI , gunicorn 的 gaiohttp worker 是基于 WSGI 的
aiohttp 2.0 也废弃了 WSGI 支持,估计以后 gaiohttp 也没法用了。
http://aiohttp.readthedocs.io/en/latest/migration.html#wsgi
问一下 sanic 跑同步的 wsgi 不会浪费性能吗
#7 并不是同步跑 WSGI ,自定义 gunicorn worker 跑 asyncio event loop
ORM 支持怎么样了?楼主有打算在生产环境用 sanic 吗?
#9 ORM 支持不咋的,有在生产环境使用,一个并不涉及到数据库的项目:
https://github.com/bosondata/prerender

