https://www.yuanrenxue.com/sanic/sanic-extension.html
Sanic-Plugins-Framework:轻松创建和使用Sanic插件的库。
Limiter:为sanic添加频率限制。
sanic-prometheus:Sanic的Prometheus指标。sanic-zipkin:通过aiozipkin轻松向zipkin / jaeger报告请求/功能/ RPC跟踪。
https://www.yuanrenxue.com/sanic/sanic-start.html
https://sanic.readthedocs.io/en/latest/sanic/asyncio_python37.html
我觉得后台任务没问题
https://sanic.readthedocs.io/en/latest/sanic/middleware.html
如果您希望安排后台任务在循环开始后运行,则Sanic提供了add_task方法可以轻松地做到这一点。async def notify_server_started_after_five_seconds():
await asyncio.sleep(5)
print(‘Server successfully started!’)
app.add_task(notify_server_started_after_five_seconds())
sanic的loop和asyncio的loop到底是不是同一个loop
import asyncioimport socketimport os
from sanic import Sanicfrom sanic.response import json
app = Sanic(name)
@app.route("/")async def test(request):
return json({“hello”: “world”})
server_socket = ‘/tmp/sanic.sock’
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove(server_socket)finally:
sock.bind(server_socket)
if name == “main”:
loop = asyncio.get_event_loop()
srv_coro = app.create_server(
sock=sock,
return_asyncio_server=True,
asyncio_server_kwargs=dict(
start_serving=False
)
)
srv = loop.run_until_complete(srv_coro)
try:
assert srv.is_serving() is False
loop.run_until_complete(srv.start_serving())
assert srv.is_serving() is True
loop.run_until_complete(srv.serve_forever())
except KeyboardInterrupt:
srv.close()
loop.close()