Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis.py: How to flush all the queries in a pipeline

I have a redis pipeline say:

r = redis.Redis(...).pipline()

Suppose I need to remove any residual query, if present in the pipeline without executing. Is there anything like r.clear()?

I have search docs and source code and I am unable to find anything.

like image 744
Mangat Rai Modi Avatar asked Aug 31 '25 16:08

Mangat Rai Modi


1 Answers

The command list is simply a python list object. You can inspect it like such:

from redis import StrictRedis
r = StrictRedis()
pipe = r.pipeline()
pipe.set('KEY1', 1)
pipe.set('KEY2', 2)
pipe.set('KEY3', 3)
pipe.command_stack
[(('SET', 'KEY1', 1), {}), (('SET', 'KEY2', 2), {}), (('SET', 'KEY3', 3), {})]

This has not yet been sent to the server so you can just pop() or remove the commands you don't want. You can also just assign an empty list, pipe.command_stack = [].

If there is a lot you could simply just re-assign a new Pipeline object to pipe.

Hope this is what you meant.

Cheers Joe

like image 193
Joe Doherty Avatar answered Sep 03 '25 18:09

Joe Doherty