def use_connection(redis=None, **kwargs): """Clears the stack and uses the given connection. Protects against mixed use of use_connection() and stacked connection contexts. """ assert len(_connection_stack) <= 1, \ 'You should not mix Connection contexts with use_connection()' release_local(_connection_stack) if redis is None: redis = yield from create_redis(**kwargs) push_connection(redis)
def coroutine(loop, kwargs): redis = yield from find_connection(loop) push_connection(redis) if 'redis' in kwargs: kwargs['redis'] = redis if 'loop' in kwargs: kwargs['loop'] = loop try: yield from asyncio.coroutine(f)(**kwargs) except Exception: raise else: connection = pop_connection() assert connection == redis, ( 'Wow, something really nasty happened to the ' 'Redis connection stack. Check your setup.') finally: yield from redis.flushdb() release_local(_connection_stack)