from aiohttp import web
from model import User, db
import asyncio #Unnecessary
routes = web.RouteTableDef()
@routes.get('/')
async def index(request):
await asyncio.sleep(1.5) #Для проверки асинхронности обработки подключений
async with db.with_bind('postgresql://localhost/postgres') as engine:
# await User.delete.gino.all() #Дабы не плодить записи в таблице на этапе разработки
await User.create(name='jack', fullname='Jack Jones')
print(await User.query.gino.all())
with open('app.html') as f:
return web.Response(text=f.read(), content_type='text/html')
dropdb postgres
createdb postgres
from aiohttp import web
from model import User
# from db_init import db
from gino import Gino
import asyncio #Unnecessary
routes = web.RouteTableDef()
db = Gino() #Создание ссылки на базу данных
@routes.get('/')
async def index(request):
# await User.delete.gino.all() #Дабы не плодить записи в таблице на этапе разработки
await asyncio.sleep(1.5) #Для проверки асинхронности обработки подключений
await User.create(name='jack', fullname='Jack Jones')
print(await User.query.gino.all())
with open('app.html') as f:
return web.Response(text=f.read(), content_type='text/html')
Error handling request
Traceback (most recent call last):
File "/Users/mac/Documents/web/myapp/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 378, in start
resp = await self._request_handler(request)
File "/Users/mac/Documents/web/myapp/venv/lib/python3.7/site-packages/aiohttp/web_app.py", line 341, in _handle
resp = await handler(request)
File "/Users/mac/Documents/web/myapp/view.py", line 17, in index
await User.create(name='jack', fullname='Jack Jones')
File "/Users/mac/Documents/web/myapp/venv/lib/python3.7/site-packages/gino/crud.py", line 446, in _create_without_instance
return await cls(**values)._create(bind=bind, timeout=timeout)
File "/Users/mac/Documents/web/myapp/venv/lib/python3.7/site-packages/gino/crud.py", line 475, in _create
row = await bind.first(q)
AttributeError: 'NoneType' object has no attribute 'first'
from aiohttp import web
from gino import Gino
import asyncio #Unnecessary
routes = web.RouteTableDef()
db = Gino() #Создание ссылки на базу данных
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
fullname = db.Column(db.String)
def __init__(self, *args, **kwargs):
super(User, self).__init__(*args, **kwargs)
loop = asyncio.get_event_loop() #Достаём event loop
async def main():
async with db.with_bind('postgresql://localhost/postgres') as engine:
await db.gino.create_all()
loop.run_until_complete(main()) #Запуск ORM Gino - помещаем экземпляр Gino в event loop
@routes.get('/')
async def index(request):
async with db.with_bind('postgresql://localhost/postgres') as engine:
# await User.delete.gino.all() #Дабы не плодить записи в таблице на этапе разработки
await asyncio.sleep(1.5) #Для проверки асинхронности обработки подключений
await User.create(name='jack', fullname='Jack Jones')
print(await User.query.gino.all())
with open('app.html') as f:
return web.Response(text=f.read(), content_type='text/html')
import asyncio
async def func1():
print(loop1)
async def func2():
print(loop2)
loop1 = asyncio.get_event_loop()
loop2 = asyncio.get_event_loop()
loop1.run_until_complete(func1())
loop1.run_until_complete(func2())
<_UnixSelectorEventLoop running=True closed=False debug=False>
<_UnixSelectorEventLoop running=True closed=False debug=False>
loop1 = asyncio.get_event_loop()
loop2 = asyncio.get_event_loop()
loop1.run_until_complete(func1())
loop1.run_until_complete(func2())