BRPOP
Since 1.1.0Lists
Syntax
BRPOP key [key ...] timeoutPops the last element of the first non-empty list among the keys given. If all of them are empty, the connection waits until another client pushes to one of them, or until timeout seconds have passed. It is the blocking form of RPOP, and the usual way to build a work queue: producers LPUSH jobs, and workers wait on BRPOP instead of polling. BLPOP pops the first element instead.
key ...: checked in the order given; the first one holding elements is popped.timeout: seconds to wait, with decimals allowed (0.5).0waits for ever.
Clients waiting on the same list are served in the order they started waiting, one element each. A push of several elements at once is served after the whole push, so a waiting BRPOP gets the last element of the list as it stands then. A list emptied by the pop is deleted.
If the key is set to another type while a client waits, the client keeps waiting. A waiting client that disconnects takes nothing: a later push stays in the list. Inside a MULTI transaction, BRPOP does not wait, and replies nil at once if every list is empty. For a safer queue, which keeps a copy of the job until it is done, use BLMOVE.
Reply
- A two-element array: the key popped from, and the element.
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.WRONGTYPE Operation against a key holding the wrong kind of valueif a key checked before a non-empty list is not a list.
Examples
> RPUSH q a b c
(integer) 3
> BRPOP empty q 1
1) "q"
2) "c"
> BRPOP empty 0.5
(nil)
A worker waiting, while another client pushes three jobs at once:
client 1> BRPOP jobs 5
client 2> LPUSH jobs j1 j2 j3
(integer) 3
client 1>
1) "jobs"
2) "j1"