Last active
December 27, 2017 20:06
-
-
Save flip111/4ba2715758e17231b874aa25c88160b7 to your computer and use it in GitHub Desktop.
using aiohttp together with other asyncio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# other stuff before this (imports & other code) | |
runner = run_app_nonblocking.setup_app(app, host='localhost', port=8080) | |
# Extra asyncio stuff here | |
loop = asyncio.get_event_loop() | |
# maybe add another site | |
extra_site = web_runner.TCPSite(runner, "127.0.0.1", 8099, | |
shutdown_timeout=60.0, | |
ssl_context=None, | |
backlog=128) | |
loop.run_until_complete(extra_site.start()) | |
# run the loop | |
run_app_nonblocking.run_loop(runner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aiohttp import helpers, web_runner | |
from aiohttp.log import access_logger | |
import asyncio | |
def setup_app(app, *, host=None, port=None, path=None, sock=None, | |
shutdown_timeout=60.0, ssl_context=None, | |
print=print, backlog=128, access_log_class=helpers.AccessLogger, | |
access_log_format=None, access_log=access_logger, | |
handle_signals=True): | |
"""Run an app locally""" | |
loop = asyncio.get_event_loop() | |
runner = web_runner.AppRunner(app, handle_signals=handle_signals, | |
access_log_class=access_log_class, | |
access_log_format=access_log_format, | |
access_log=access_log) | |
loop.run_until_complete(runner.setup()) | |
sites = [] | |
if host is not None: | |
if isinstance(host, (str, bytes, bytearray, memoryview)): | |
sites.append(web_runner.TCPSite(runner, host, port, | |
shutdown_timeout=shutdown_timeout, | |
ssl_context=ssl_context, | |
backlog=backlog)) | |
else: | |
for h in host: | |
sites.append(web_runner.TCPSite(runner, h, port, | |
shutdown_timeout=shutdown_timeout, | |
ssl_context=ssl_context, | |
backlog=backlog)) | |
elif path is None and sock is None or port is not None: | |
sites.append(web_runner.TCPSite(runner, port=port, | |
shutdown_timeout=shutdown_timeout, | |
ssl_context=ssl_context, backlog=backlog)) | |
if path is not None: | |
if isinstance(path, (str, bytes, bytearray, memoryview)): | |
sites.append(web_runner.UnixSite(runner, path, | |
shutdown_timeout=shutdown_timeout, | |
ssl_context=ssl_context, | |
backlog=backlog)) | |
else: | |
for p in path: | |
sites.append(web_runner.UnixSite(runner, p, | |
shutdown_timeout=shutdown_timeout, | |
ssl_context=ssl_context, | |
backlog=backlog)) | |
if sock is not None: | |
if not isinstance(sock, Iterable): | |
sites.append(web_runner.SockSite(runner, sock, | |
shutdown_timeout=shutdown_timeout, | |
ssl_context=ssl_context, | |
backlog=backlog)) | |
else: | |
for s in sock: | |
sites.append(web_runner.SockSite(runner, s, | |
shutdown_timeout=shutdown_timeout, | |
ssl_context=ssl_context, | |
backlog=backlog)) | |
for site in sites: | |
loop.run_until_complete(site.start()) | |
return runner | |
def run_loop(runner): | |
loop = asyncio.get_event_loop() | |
try: | |
if print: # pragma: no branch | |
names = sorted(str(s.name) for s in runner.sites) | |
print("======== Running on {} ========\n" | |
"(Press CTRL+C to quit)".format(', '.join(names))) | |
loop.run_forever() | |
except (GracefulExit, KeyboardInterrupt): # pragma: no cover | |
pass | |
finally: | |
loop.run_until_complete(runner.cleanup()) | |
if hasattr(loop, 'shutdown_asyncgens'): | |
loop.run_until_complete(loop.shutdown_asyncgens()) | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment