Skip to content

Commit

Permalink
Sanic is now fully async!
Browse files Browse the repository at this point in the history
  • Loading branch information
natfarleydev committed Jan 16, 2017
1 parent f3b9c0f commit e651fd6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
3 changes: 3 additions & 0 deletions beards/apibeard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# from multiprocessing import Process

import sanic
assert sanic.__version__ == "0.1.7", "Due to reasons detailed in https://github.com/channelcat/sanic/issues/275, this app currently only supports Sanic version 0.1.7."

from .apibeard import APIBeard
67 changes: 54 additions & 13 deletions beards/apibeard/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

from multiprocessing import Process

# for https://github.com/channelcat/sanic/issues/275
import asyncio
import sanic
from sanic.server import HttpProtocol

from .app import app
from .telegram import setup_telegram
Expand All @@ -16,6 +20,38 @@ def _start_server(the_app, *args, **kwargs):
return the_app.run(*args, **kwargs)


# from https://github.com/channelcat/sanic/issues/275
async def run_web_app(app, port, *, loop, logger, request_timeout=20):
connections = {}
signal = sanic.server.Signal()
handler_factory = lambda: HttpProtocol(
loop = loop,
connections = connections,
signal = signal,
request_handler = app.handle_request,
request_timeout = request_timeout,
request_max_size = 1024*1024
)

server = await loop.create_server(handler_factory, None, port)
for sock in server.sockets:
sockname = sock.getsockname()
logger.info("Listening on %s:%d", sockname[0], sockname[1])

try:
await asyncio.Future(loop=loop)
finally:
server.close()
await server.wait_closed()

# Complete all tasks on the loop
signal.stopped = True
for connection in connections.keys():
connection.close_if_idle()
while connections:
await asyncio.sleep(0.1, loop=loop)


def start(debug=False):
"""Starts the Sanic server.
Expand All @@ -25,19 +61,24 @@ def start(debug=False):
Returns the started process.
"""
global proc
proc = Process(
target=_start_server,
args=(
app,
),
kwargs=dict(
debug=debug,
after_start=setup_telegram
)
)
proc.start()
return proc
# global proc
# proc = Process(
# target=_start_server,
# args=(
# app,
# ),
# kwargs=dict(
# debug=debug,
# after_start=setup_telegram
# )
# )
# proc.start()
# return proc
import logging
logger = logging.getLogger("async_sanic")

coro = run_web_app(app, 8000, loop=asyncio.get_event_loop(), logger=logger)
return asyncio.ensure_future(coro)


def stop():
Expand Down
9 changes: 8 additions & 1 deletion beards/apibeard/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from sanic.response import json, text
from sanic.exceptions import NotFound

from skybeard.beards import Beard

from . import telegram as tg
from .. import database
# from . import utils
Expand All @@ -18,7 +20,12 @@

@app.route('/')
async def hello_world(request):
return text("Hello World! Your API beard is working! Running Sanic version: {}.".format(sanic.__version__))
return text(
("Hello World! Your API beard is working! "
"Running Sanic version: {}. Beards running: {}.").format(
sanic.__version__,
Beard.beards
))


@key_blueprint.route('/relay/<method:[A-z]+>', methods=["POST", "GET"])
Expand Down

0 comments on commit e651fd6

Please sign in to comment.