File tree Expand file tree Collapse file tree 1 file changed +12
-6
lines changed Expand file tree Collapse file tree 1 file changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -438,20 +438,26 @@ Web service that computes the requested power of two.
438438 text = " 2 ^ {} is {} " .format(power, result))
439439
440440
441- async def init_app ():
441+ async def init_db (app ):
442+ """ Initialize a connection pool."""
443+ app[' pool' ] = await asyncpg.create_pool(database = ' postgres' ,
444+ user = ' postgres' )
445+ yield
446+ app[' pool' ].close()
447+
448+
449+ def init_app ():
442450 """ Initialize the application server."""
443451 app = web.Application()
444- # Create a database connection pool
445- app[' pool' ] = await asyncpg.create_pool(database = ' postgres' ,
446- user = ' postgres' )
452+ # Create a database context
453+ app.cleanup_ctx.append(init_db)
447454 # Configure service routes
448455 app.router.add_route(' GET' , ' /{power: \d+} ' , handle)
449456 app.router.add_route(' GET' , ' /' , handle)
450457 return app
451458
452459
453- loop = asyncio.get_event_loop()
454- app = loop.run_until_complete(init_app())
460+ app = init_app()
455461 web.run_app(app)
456462
457463 See :ref: `asyncpg-api-pool ` API documentation for more information.
You can’t perform that action at this time.
0 commit comments