BLMOVE
Since 1.1.0Lists
Syntax
BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeoutMoves an element from one list to another in one step, waiting for one if source is empty. It is the blocking form of LMOVE: if source has no elements, the connection waits until another client pushes to it, or until timeout seconds have passed.
- The first
LEFT|RIGHTis the end ofsourceto take from; the second, the end ofdestinationto put it on. timeout: seconds to wait, with decimals allowed.0waits for ever.
It makes a reliable queue. A worker moves a job from the shared jobs list to a list of its own, BLMOVE jobs work RIGHT LEFT 0, and removes it with LREM once done. The job is never only in the worker's memory: if the worker dies, it is still in work, to be retried.
destination is created if missing. source and destination may be the same list, which rotates it. If destination holds another type, the command fails with WRONGTYPE and nothing moves; while source is empty, though, it waits as usual. Clients waiting on one list are served in the order they started waiting. Inside a MULTI transaction it does not wait, and replies nil if source is empty. BRPOPLPUSH is the older form, for RIGHT LEFT.
Reply
- The element moved.
nilif the timeout passed.
Errors
ERR timeout is negative.ERR timeout is not a float or out of rangeif the timeout is not a number.ERR syntax errorfor a direction other thanLEFTorRIGHT.WRONGTYPE Operation against a key holding the wrong kind of valueifsource, ordestinationwhen there is an element to move, is not a list.
Examples
> RPUSH r a b c
(integer) 3
> BLMOVE r r LEFT RIGHT 0
"a"
> LRANGE r 0 -1
1) "b"
2) "c"
3) "a"
> BLMOVE empty dst LEFT RIGHT 0.5
(nil)
A worker waiting for a job while another client adds one:
client 1> BLMOVE jobs work RIGHT LEFT 5
client 2> LPUSH jobs j1
(integer) 1
client 1>
"j1"
client 2> LRANGE work 0 -1
1) "j1"