I've been working with Redis pipelines (sets of operations run in bulk to reduce the socket i/o cost of individual operations) in pyredis.
Sometimes my pipelines get very large and accumulate too much RAM, so I'd like to execute the pipeline.execute()
method to run the operations stored in the pipeline when the given pipeline has accumulated N operations.
This raises the question: How can one measure the number of operations in a Redis pipeline? I know I could store a counter variable, but would prefer to just query the pipeline to determine how many operations are in it. Any help others can provide on this question will be very helpful!
Yes, you could use the len(pipeline)
to get the number of operations. In redis-py/client.py, there is a definition of class BasePipeline
, and it implements the __len__
method: get the length of its command_stack, which records all the command piped.
def __len__(self):
return len(self.command_stack)
E.g.
>>> import redis
>>> redis = redis.StrictRedis()
>>> p = redis.pipeline()
>>> len(p)
0
>>> p.set('test',1)
StrictPipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
>>> len(p)
1