import asyncio
import logging
import pathlib

import aiohttp_jinja2
import jinja2
from aiohttp import web

from aiohttpdemo_polls.routes import setup_routes
from aiohttpdemo_polls.utils import init_postgres, load_config
from aiohttpdemo_polls.views import SiteHandler


PROJ_ROOT = pathlib.Path(__file__).parent.parent
TEMPLATES_ROOT = pathlib.Path(__file__).parent / 'templates'
log = logging.getLogger(__name__)


async def init(loop):
    # setup application and extensions
    app = web.Application(loop=loop)
    aiohttp_jinja2.setup(
        app, loader=jinja2.FileSystemLoader(str(TEMPLATES_ROOT)))
    # load config from yaml file
    conf = load_config(str(PROJ_ROOT / 'config' / 'polls.yaml'))

    # create connection to the database
    pg = await init_postgres(conf['postgres'], loop)

    async def close_pg():
        pg.close()
        await pg.wait_closed()

    app.register_on_finish(close_pg)

    # setup views and routes
    handler = SiteHandler(pg)
    setup_routes(app, handler, PROJ_ROOT)

    # init logging and attach access_log
    logging.basicConfig(level=logging.DEBUG)
    app_handler = app.make_handler(access_log=log)
    host, port = conf['host'], conf['port']
    srv = await loop.create_server(app_handler, host, port)
    print("Server started at http://{0}:{1}".format(host, port))
    return srv, app_handler


def main():
    loop = asyncio.get_event_loop()
    srv, app_handler = loop.run_until_complete(init(loop))

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        srv.close()
        loop.run_until_complete(srv.wait_closed())
        loop.run_until_complete(app.shutdown())
        loop.run_until_complete(handler.finish_connections(shutdown_timeout))
        loop.run_until_complete(app.finish())
    loop.close()


if __name__ == '__main__':
    main()

@@ -377,42 +377,8 @@
 es'%0A
-log = logging.getLogger(__name__)%0A
 %0A%0Aas
@@ -781,16 +781,19 @@
 lose_pg(
+app
 ):%0A     
@@ -850,26 +850,25 @@
 app.
-register_on_finish
+on_cleanup.append
 (clo
@@ -981,94 +981,8 @@
 T)%0A%0A
-    # init logging and attach access_log%0A    logging.basicConfig(level=logging.DEBUG)%0A
     
@@ -1080,170 +1080,109 @@
     
-srv = await loop.create_server(app_handler, host, port)%0A    print(%22Server started at http://%7B0%7D:%7B1%7D%22.format(host, port))%0A    return srv, app_handler%0A%0A%0Adef main():
+return app, host, port%0A%0A%0Adef main():%0A    # init logging%0A    logging.basicConfig(level=logging.DEBUG)%0A
 %0A   
@@ -1222,24 +1222,23 @@
     
-srv, app_handler
+app, host, port
  = l
@@ -1276,359 +1276,49 @@
 p))%0A
-%0A
     
-try:%0A        loop.run_forever()%0A    except KeyboardInterrupt:%0A        pass%0A    finally:%0A        srv.close()%0A        loop.run_until_complete(srv.wait_closed())%0A        loop.run_until_complete(app.shutdown())%0A        loop.run_until_complete(handler.finish_connections(shutdown_timeout))%0A        loop.run_until_complete(app.finish())%0A    loop.close(
+web.run_app(app, host=host, port=port
 )%0A%0A%0A

Use run_app im demo