Question

I am reading a book on Redis (Redis in action) and on page 59-60. There is an example use for transaction as below:


def trans(conn):
    pipeline = conn.pipeline()
    pipeline.incr('trans:')
    time.sleep(.1)
    pipeline.incr('trans:',-1)
    print pipeline.execute()[0]

def run_transaction(conn):
    if 1:
        for i in xrange(3):
            threading.Thread(target=trans, args =(conn,)).start()
        time.sleep(.5)

I am expecting that this produces:

0

0

0

But the output is:

1

1

1

Can someone explain why ('trans: is never used anywhere else')? Thanks

Was it helpful?

Solution

The result of 'pipeline.execute()' is an array of 2 elements. The first is the result of 'pipeline.incr('trans:')', the second is the result of 'pipeline.incr('trans:',-1)'. The output is correct in your case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top