我尝试学习aiohttp,但是peewee-async / aiopg出现了问题
import settings
import jinja2
import aiohttp_jinja2
import peewee
import logging
from peewee_async import Manager, PostgresqlDatabase
from aiohttp import web
logging.basicConfig(level=logging.DEBUG)
database = PostgresqlDatabase(
settings.DB_NAME,
user=settings.DB_USER,
password=settings.DB_PASSWORD,
host=settings.DB_HOST
)
routes = web.RouteTableDef()
class UserModel(peewee.Model):
role_choices = (
(0, "User"),
(1, "Admin")
)
sex_choices = (
(0, "Male"),
(1, "Female")
)
id = peewee.PrimaryKeyField(
null=True
)
username = peewee.CharField(
max_length=255
)
email = peewee.CharField(
max_length=255,
unique=True,
constraints=[peewee.Check("email ~* '^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+[.][A-Za-z]+$'")],
index=True
)
role = peewee.SmallIntegerField(
choices=role_choices,
default=0
)
password = peewee.CharField(
max_length=255
)
sex = peewee.SmallIntegerField(
null=True,
choices=sex_choices
)
age = peewee.SmallIntegerField(
null=True
)
class Meta:
database = database
@routes.get('/')
async def hello(request):
await request.app.objects.create(
UserModel,
username="admin",
email="admin@admin.com",
role=1,
password="admin123",
sex=0,
age=26
)
user = await request.app.objects.get(UserModel, email="admin@admin.com")
context = {'user': user}
response = aiohttp_jinja2.render_template('base.html',
request,
context)
response.headers['Content-Language'] = 'ru'
return response
if __name__ == '__main__':
app = web.Application()
app.add_routes(routes)
app.database = database
app.database.set_allow_sync(False)
app.objects = Manager(app.database)
#database.create_table(UserModel)
aiohttp_jinja2.setup(app,
loader=jinja2.FileSystemLoader(settings.TEMPLATE_DIR))
web.run_app(app, host=settings.HOST, port=settings.PORT)
当我切换到“ localhost”时发生错误。在对数据库的任何请求阶段,无论是创建还是选择。肯定有到数据库的连接,我创建了一个表并尝试从pg_catalog中进行选择。错误文字:
^Croot@933eff50aca7:/app# python main.py
DEBUG:asyncio:Using selector: EpollSelector
======== Running on http://0.0.0.0:80 ========
(Press CTRL+C to quit)
DEBUG:peewee.async:('INSERT INTO "usermodel" ("username", "email", "role", "password", "sex", "age") VALUES (%s, %s, %s, %s, %s, %s
) RETURNING "id"', (['admin', 'admin@admin.com', 1, 'admin123', 0, 26],), {})
/usr/local/lib/python3.7/site-packages/peewee_async.py:1078: RuntimeWarning: coroutine 'Pool.from_pool_fill' was never awaited
**self.connect_kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
ERROR:aiohttp.server:Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
resp = await task
File "/usr/local/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
resp = await handler(request)
File "main.py", line 73, in hello
age=26
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 181, in create
pk = yield from self.execute(query)
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 269, in execute
return (yield from execute(query))
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 433, in execute
return (yield from coroutine(query))
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 597, in insert
cursor = yield from _execute_query_async(query)
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 1558, in _execute_query_async
return (yield from _run_sql(query.database, *query.sql()))
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 1537, in _run_sql
cursor = yield from database.cursor_async()
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 921, in cursor_async
yield from self.connect_async(loop=self._loop)
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 907, in connect_async
yield from conn.connect()
File "/usr/local/lib/python3.7/site-packages/peewee_async.py", line 1078, in connect
**self.connect_kwargs)
TypeError: '_PoolContextManager' object is not iterable
使用docker-compose。软件包版本:
aiodns==2.0.0
aiohttp==3.6.2
aiohttp-jinja2==1.2.0
aiopg==1.0.0
async-timeout==3.0.1
attrs==19.3.0
cchardet==2.1.5
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
idna==2.8
Jinja2==2.10.3
MarkupSafe==1.1.1
multidict==4.7.1
peewee==2.10.2
peewee-async==0.5.12
pipenv==2018.10.13
psycopg2-binary==2.8.4
pycares==3.1.0
pycparser==2.19
virtualenv==16.7.9
virtualenv-clone==0.5.3
yarl==1.4.2