Python3 aioredis升級到2.0后create_redis_pool連接問題


aioredis升級到2.0后,會發現create_redis_pool來創建連接池的方法無法使用,源碼中也找不到這個方法了。

 

通過官方文檔查看得知,2.0后已經舍棄了create_redis_pool的連接方式,整個核心和公共 API 已被重新編寫,以盡可能地遵循redis-py的實現不再通過調用來連接到 redis await aioredis.create_pool(...)。相反,默認的初始化模式與redis-py相同:

 

 

連接示例:

import asyncio
import aioredis


async def main():
    # Redis client bound to single connection (no auto reconnection).
    redis = aioredis.from_url(
        "redis://localhost", encoding="utf-8", decode_responses=True
)

    async with redis.client() as conn:
        await conn.set("my-key", "value")
        val = await conn.get("my-key")
    print(val)

async def redis_pool():
    # Redis client bound to pool of connections (auto-reconnecting).
    redis = aioredis.from_url(
        "redis://localhost", encoding="utf-8", decode_responses=True
    )
    await redis.set("my-key", "value")
    val = await redis.get("my-key")
    print(val)

if __name__ == "__main__":
    asyncio.run(main())
    asyncio.run(redis_pool())

 

如果redis更改了端口,密碼或者需要指定DB等,像pyredis一樣直接指定參數即可

redis = aioredis.from_url(
    "redis://127.0.0.1", port=44117, password='qwaszx', db=2, encoding="utf-8", decode_responses=True
)

 

參考:

https://aioredis.readthedocs.io/en/latest/migration/#connecting-to-redis

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM