def __init__(self, conn_address=str, key_prefix='', loop=None): # conn = loop.run_until_complete(aioredis.create_pool(settings.REDIS_ADDRESS)) conn = aioredis.ConnectionsPool(address=conn_address, minsize=1, maxsize=10, loop=loop or asyncio.get_event_loop()) self._redis = aioredis.Redis(conn) self._rk = key_prefix+RK_STORAGE_DATA if (not key_prefix) or key_prefix.endswith(':') \ else key_prefix+':'+RK_STORAGE_DATA
async def test_connect_pool_aioredis_instance(self): def awaiter(self): yield from [] with patch('aioredlock.redis.Instance._create_redis_pool') as create_redis_pool, \ patch.object(aioredis.Redis, '__await__', awaiter): redis_connection = aioredis.Redis( aioredis.ConnectionsPool(('127.0.0.1', 6379), minsize=1, maxsize=100), ) instance = Instance(redis_connection) await instance.connect() assert not create_redis_pool.called
async def test_connect_pool_aioredis_instance_with_sentinel(self): def awaiter(self): yield from [] with patch('aioredlock.redis.Instance._create_redis_pool') as create_redis_pool, \ patch.object(aioredis.Redis, '__await__', awaiter): sentinel = Sentinel(('127.0.0.1', 26379), master='leader') redis_connection = aioredis.Redis( aioredis.ConnectionsPool(('127.0.0.1', 6379), minsize=1, maxsize=100), ) with patch.object(sentinel, 'get_master', return_value=asyncio.Future()) as mock_redis: mock_redis.return_value.set_result(redis_connection) instance = Instance(sentinel) await instance.connect() assert not create_redis_pool.called
Description: Author: zhengchengzhong Date: 2021-01-26 20:32:43 ''' import uvicorn import asyncio import aioredis from pathlib import Path from fastapi import FastAPI from aioredis import Redis # 创建一个redis连接池 redis_pool = aioredis.ConnectionsPool("redis://localhost", password=None, minsize=1, maxsize=10) app = FastAPI() @app.get("/") def index(): return {"message": "Hello world"} @app.get("/red") async def red(): print("request comming") await asyncio.sleep(3) # 连接池获取一个连接
def __init__(self, conn_address=str, key_prefix='', loop=None): # conn = loop.run_until_complete(aioredis.create_pool(settings.REDIS_ADDRESS)) conn = aioredis.ConnectionsPool(address=conn_address, minsize=1, maxsize=10, loop=loop or asyncio.get_event_loop()) self._kp = key_prefix self._redis = aioredis.Redis(conn)
async def init_redis(self, *args, **kwargs): """redis连接池初始化函数""" return await aioredis.Redis(aioredis.ConnectionsPool(*args, **kwargs))
import aioredis from aioredis import Redis from fastapi import FastAPI app = FastAPI() # 这是默认的同步方式 @app.get('/') def index(): """某IO操作 10s""" return {'message': 'hello world'} REDIS_POOL = aioredis.ConnectionsPool('redis://47.193.14.198:6379', password='******', minsize=1, maxsize=10) @app.get('/red') async def red(): """ 异步操作接口 """ print('语法来了') await asyncio.sleep(3) # 连接池获取一个连接 conn = await REDIS_POOL.acquire() redis = Redis(conn) # 设置值
''' ''' import uvicorn import aioredis from aioredis import Redis from fastapi import FastAPI import asyncio app = FastAPI() #创建一个redis连接池 REDIS_POOL = aioredis.ConnectionsPool("redis://47.193.14.198:6379", password="******", minsize=1, maxsize=10) @app.get('/') def index(): '''普通接口''' return {"message": "Hello world"} @app.get("/red") async def red(): '''异步操作接口''' print("请求来了") await asyncio.sleep(3) #连接池获取一个连接 conn = await REDIS_POOL.acquire() redis = Redis(conn)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # __author__ : YiXuan # __date__ : 11/30/2020 3:43 PM # __software__ : PyCharm import asyncio import uvicorn import aioredis from aioredis import Redis from fastapi import FastAPI app = FastAPI() REDIS_POOL = aioredis.ConnectionsPool('redis://127.0.0.1:6379', password=None, minsize=1, maxsize=10) @app.get('/') def index(): '''普通操作接口''' return {'message': 'hello world!'} @app.get('/red') async def red(): '''异步操作接口''' print('请求来了') await asyncio.sleep(1)
def __init__(self): self.redis = aioredis.ConnectionsPool('redis://localhost/1', minsize=1, maxsize=3)
def make_app(cache_cls=None, database_pool=None, debug=False): if options.dummy and not debug: raise RuntimeError("Dummy backend is only allowed in debug mode") async_redis_pool = aioredis.ConnectionsPool( address=(options.redis_host, options.redis_port), minsize=5, maxsize=100, loop=ioloop.IOLoop.current().asyncio_loop, ) redis_pool = redis.ConnectionPool(host=options.redis_host, port=options.redis_port) def get_auth_class(): if options.dummy_auth: return auth.DummyAuth else: return auth.Auth get_sub = partial(pubsub.AsyncRedisSubscribe, async_redis_pool) publish = partial(pubsub.redis_publish, async_redis_pool) if cache_cls is None: def cache_cls(): return partial(cache.RedisCache, connection_pool=redis_pool) if options.dummy: dummy_dir = tempfile.mkdtemp() print('Dummy storage path:', dummy_dir) def get_transfer_cls(): if options.dummy: return partial(LocalTransfer, dummy_dir) if options.local_storage: return partial(LocalTransfer, options.local_storage) return S3Transfer if database_pool is None: database_pool = SimpleConnectionPool(1, 20, dsn=options.psql_dsn) transfer_connector = TransferConnector( concurrent_transfers=options.transfers, get_cache_cls=cache_cls, transfer_cls=get_transfer_cls, ) prefix = r'(?P<prefix>[\d\w-]+)' file = r'/(?P<file_path>[/\d\w-]+)' application = Application( [(r'^/api/v0/files/' + prefix + file, FileHandler, dict( publish=publish, transfer_cls=get_transfer_cls, get_auth_cls=get_auth_class, get_cache_cls=cache_cls, database_pool=database_pool, transfer_connector=transfer_connector, )), (r'^/api/v0/websocket/' + prefix + file, FileWebSocketHandler, dict(get_sub=get_sub, )), (r'^/api/v0/websocket/' + prefix, PrefixWebSocketHandler, dict( get_sub=get_sub, get_auth_cls=get_auth_class, get_cache_cls=cache_cls, database_pool=database_pool, )), (r'^/api/v0/prefix/', PrefixHandler, dict( get_cache_cls=cache_cls, get_auth_cls=get_auth_class, database_pool=database_pool, )), (r'^/api/v0/quota/', QuotaHandler, dict( get_cache_cls=cache_cls, get_auth_cls=get_auth_class, database_pool=database_pool, ))], debug=debug) return application