-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
429 additions
and
112 deletions.
There are no files selected for viewing
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
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
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from aiohttp import web | ||
import aiohttp_sqlalchemy | ||
from aiohttp_sqlalchemy import sa_decorator, sa_engine, sa_middleware | ||
from datetime import datetime | ||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
from sqlalchemy.ext.asyncio import create_async_engine | ||
from random import choice | ||
|
||
|
||
metadata = sa.MetaData() | ||
Base = orm.declarative_base(metadata=metadata) | ||
|
||
|
||
class Request(Base): | ||
__tablename__ = 'requests' | ||
id = sa.Column(sa.Integer, primary_key=True) | ||
timestamp = sa.Column(sa.DateTime(), default=datetime.now) | ||
|
||
|
||
@sa_decorator('sa_secondary') | ||
async def main(request): | ||
async with request.app['sa_main'].begin() as conn: | ||
await conn.run_sync(Base.metadata.create_all) | ||
|
||
async with request.app['sa_secondary'].begin() as conn: | ||
await conn.run_sync(Base.metadata.create_all) | ||
|
||
session = choice(['sa_main', 'sa_secondary']) | ||
|
||
async with request[session].begin(): | ||
request[session].add_all([Request()]) | ||
|
||
async with request['sa_main'].begin(): | ||
result = await request['sa_main'].execute(sa.select(Request)) | ||
main_result = {r.id: r.timestamp.isoformat() for r in result.scalars()} | ||
|
||
async with request['sa_secondary'].begin(): | ||
result = await request['sa_secondary'].execute(sa.select(Request)) | ||
secondary_result = {r.id: r.timestamp.isoformat() for r in result.scalars()} | ||
|
||
data = { | ||
'main': main_result, | ||
'secondary': secondary_result, | ||
} | ||
return web.json_response(data) | ||
|
||
|
||
app = web.Application(middlewares=[ | ||
sa_middleware(), | ||
]) | ||
aiohttp_sqlalchemy.setup(app, [ | ||
sa_engine(create_async_engine('sqlite+aiosqlite:///')), | ||
sa_engine(create_async_engine('sqlite+aiosqlite:///'), 'sa_secondary'), | ||
]) | ||
app.add_routes([web.get('/', main)]) | ||
web.run_app(app) |
Oops, something went wrong.