本文整理汇总了Python中sanic.Sanic.create_server方法的典型用法代码示例。如果您正苦于以下问题:Python Sanic.create_server方法的具体用法?Python Sanic.create_server怎么用?Python Sanic.create_server使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sanic.Sanic
的用法示例。
在下文中一共展示了Sanic.create_server方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import create_server [as 别名]
def main():
app = Sanic(__name__)
app.blueprint(ACCOUNTS_BP)
app.blueprint(ASSETS_BP)
app.blueprint(AUTH_BP)
app.blueprint(ERRORS_BP)
app.blueprint(HOLDINGS_BP)
app.blueprint(OFFERS_BP)
load_config(app)
zmq = ZMQEventLoop()
asyncio.set_event_loop(zmq)
server = app.create_server(
host=app.config.HOST, port=app.config.PORT, debug=app.config.DEBUG)
loop = asyncio.get_event_loop()
asyncio.ensure_future(server)
asyncio.ensure_future(open_connections(app))
signal(SIGINT, lambda s, f: loop.close())
try:
loop.run_forever()
except KeyboardInterrupt:
close_connections(app)
loop.stop()
示例2: Sanic
# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import create_server [as 别名]
from sanic import Sanic
from sanic.response import json
from multiprocessing import Event
from signal import signal, SIGINT
import asyncio
import uvloop
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({"answer": "42"})
asyncio.set_event_loop(uvloop.new_event_loop())
server = app.create_server(host="0.0.0.0", port=8001)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(server)
signal(SIGINT, lambda s, f: loop.stop())
try:
loop.run_forever()
except:
loop.stop()
示例3: Sanic
# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import create_server [as 别名]
}
}
app = Sanic(__name__, log_config=LOG_SETTINGS)
@app.middleware('request')
async def set_request_id(request):
request_id = request.headers.get('X-Request-ID') or str(uuid.uuid4())
context.set("X-Request-ID", request_id)
@app.route("/")
async def test(request):
log.debug('X-Request-ID: %s', context.get('X-Request-ID'))
log.info('Hello from test!')
return response.json({"test": True})
if __name__ == '__main__':
asyncio.set_event_loop(uvloop.new_event_loop())
server = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
loop = asyncio.get_event_loop()
loop.set_task_factory(context.task_factory)
task = asyncio.ensure_future(server)
try:
loop.run_forever()
except:
loop.stop()