From b89c5338182ad76a6aa44bfecaafe993081ed1a5 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Tue, 15 Jan 2019 15:04:30 +0200 Subject: [PATCH] Adding doc --- docs/index.rst | 1 + docs/sanic/asyncio_python37.rst | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 docs/sanic/asyncio_python37.rst diff --git a/docs/index.rst b/docs/index.rst index 4123d32c..180ef1ae 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -33,6 +33,7 @@ Guides sanic/changelog sanic/contributing sanic/api_reference + sanic/asyncio_python37 Module Documentation diff --git a/docs/sanic/asyncio_python37.rst b/docs/sanic/asyncio_python37.rst new file mode 100644 index 00000000..1b09c1b3 --- /dev/null +++ b/docs/sanic/asyncio_python37.rst @@ -0,0 +1,58 @@ +Python 3.7 AsyncIO examples +########################### + +With Python 3.7 AsyncIO got major update for the following types: + + - asyncio.AbstractEventLoop + - asyncio.AnstractServer + + +This example shows how to use sanic with Python 3.7, to be precise: how to retrieve an asyncio server instance: + +.. code:: python + + import asyncio + import socket + import os + + from sanic import Sanic + from 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.remote(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_args=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() + + +Please note that uvloop does not support these features yet.