2017-07-16 110 views
0

我正在運行下面的代碼。我試圖通過創建一個全局連接池對象,可以在aiohttp視圖對象中使用aiohttp使用aioredis。但是,load_redis函數正在出錯。我甚至不確定這是否與aiohttp一起使用aioredis的正確方法。aioredis + aiohttp正確使用連接池

#!/usr/bin/env python 

import asyncio 
import uvloop 
from aiohttp import web 
from settings import SERVER_IP, SERVER_PORT, REDIS_HOST, REDIS_PORT 
import aiohttp_jinja2 
import jinja2 
import aioredis 





asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) 
loop = asyncio.get_event_loop() 
app = web.Application() 
aiohttp_jinja2.setup(app, 
    loader=jinja2.FileSystemLoader('templates')) 

REDIS_CONNECTION_POOL = None 
async def load_redis(): 
    global REDIS_CONNECTION_POOL 
    REDIS_CONNECTION_POOL = await aioredis.create_pool(
     (REDIS_HOST, REDIS_PORT), 
     minsize=5, maxsize=10, 
     loop=loop) 
    await REDIS_CONNECTION_POOL.execute('set', 'current_code', '00000') 
loop.run_until_complete(load_redis()) 


#prevent import errors 
from routes import setup_routes 
setup_routes(app) 
web.run_app(app, host=SERVER_IP, port=SERVER_PORT) 

這是我看到的錯誤訊息,當我運行此:

Traceback (most recent call last): 
    File "server.py", line 29, in <module> 
    loop.run_until_complete(load_redis()) 
    File "uvloop/loop.pyx", line 1203, in uvloop.loop.Loop.run_until_complete (uvloop/loop.c:25632) 
    File "server.py", line 28, in load_redis 
    await REDIS_CONNECTION_POOL.execute('set', 'current_code', '00000') 
AttributeError: 'RedisPool' object has no attribute 'execute' 
Task was destroyed but it is pending! 
task: <Task pending coro=<RedisPool._do_close() running at /Users/brian/.pyenv/versions/3.6.0/lib/python3.6/site-packages/aioredis/pool.py:117> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10bb94b58>()]>> 

回答