redis-py raises TimeoutError while it waits for a pipeline or a blocking pop
Fix it fast
Most common cause, 1 of 3A blocking pop waits longer than the 5-second socket timeout
Give the worker that blocks its own client without a read limit, or with one longer than the block time.
import redis
worker = redis.Redis(host="localhost", port=6379, socket_timeout=None)
item = worker.blpop(["jobs"], timeout=30)Short answer
redis.exceptions.TimeoutError: Timeout reading from localhost:6379 means redis-py waited longer than socket_timeout for a reply. Since redis-py 8.0 that limit defaults to 5 seconds, so a BLPOP that blocks longer, a very large pipeline or a slow command on the server trips it. Raise socket_timeout above the longest wait, or split the pipeline. Applies to redis-py 8.x.
Which cause do you have?
Check which redis-py you run and whether the server logged slow commands. Version 8 or newer with an empty slowlog points to the client-side limit; long entries in the slowlog point to the server.
python -m pip show redis | grep -i '^version'
redis-cli SLOWLOG GET 5Then find the line you see in this list and open its fix:
- 1A blocking pop waits longer than the 5-second socket timeout
The error arrives about 5 seconds into a BLPOP or BRPOP whose own timeout is longer, or 0 - 2One pipeline carries more replies than arrive within socket_timeout
The error is raised from pipe.execute() on a bulk job, and smaller batches of the same commands succeed - 3The server itself is busy with a slow command
SLOWLOG GET shows entries whose third value is in the millions of microseconds, often KEYS or a large DEL
What does the error look like?
redis.exceptions.TimeoutError: Timeout reading from localhost:6379redis.exceptions.TimeoutError: Timeout reading from socket
Cause 1 of 3
A blocking pop waits longer than the 5-second socket timeout
You seeThe error arrives about 5 seconds into a BLPOP or BRPOP whose own timeout is longer, or 0
Check
Timeout reading from on a queue worker usually means the client gave up before the blocking command did. redis-py 8.0.0 changed socket_timeout and socket_connect_timeout to 5 seconds, and its release notes warn that a command blocking longer than the socket timeout may raise TimeoutError first. A BLPOP with timeout 30, or 0 for no limit, can then be cut off at 5 s.
import redis
r = redis.Redis()
print(redis.__version__)
r.blpop(["jobs"], timeout=30) # can raise after ~5 s on 8.x defaultsFix
Give the worker that blocks its own client without a read limit, or with one longer than the block time. Keep the 5-second default on clients that serve web requests.
import redis
worker = redis.Redis(host="localhost", port=6379, socket_timeout=None)
item = worker.blpop(["jobs"], timeout=30)Verify
On an empty list the call now returns None after the full 30 seconds instead of raising.
print(worker.blpop(["jobs"], timeout=30))Confirmed by github.com
Cause 2 of 3
One pipeline carries more replies than arrive within socket_timeout
You seeThe error is raised from pipe.execute() on a bulk job, and smaller batches of the same commands succeed
Check
A pipeline can trip the same timeout because execute() sends every buffered command and then reads all the replies, so one huge pipeline is one long wait. Redis also has to hold all those replies in memory until the client reads them; its pipelining guide suggests batches of a reasonable size, such as 10,000 commands, then reading the replies before sending more.
pipe = r.pipeline(transaction=False)
for key, value in items: # how many items is this?
pipe.set(key, value)
print(len(items))
pipe.execute()Fix
Send the work in chunks, and give the bulk client a socket_timeout that fits one chunk.
bulk = redis.Redis(socket_timeout=30)
size = 10_000
for start in range(0, len(items), size):
pipe = bulk.pipeline(transaction=False)
for key, value in items[start:start + size]:
pipe.set(key, value)
replies = pipe.execute()Verify
Each execute() returns a list with one reply per command in that chunk, and the job finishes.
print(len(replies)) # one reply per command in the last chunkConfirmed by redis.io
Cause 3 of 3
The server itself is busy with a slow command
You seeSLOWLOG GET shows entries whose third value is in the millions of microseconds, often KEYS or a large DEL
Check
When every client times out at once, the delay is on the server. Redis runs a command on one thread and serves nobody else while it executes, and the slow log records exactly that execution time. A KEYS pattern over a large keyspace is the classic case; the Redis docs warn against using KEYS in application code.
redis-cli SLOWLOG GET 10Fix
Replace KEYS with incremental SCAN iteration in the code that called it, then watch the slow log again.
for key in r.scan_iter(match="session:*", count=1000):
handle(key)Verify
After a SLOWLOG RESET, no new multi-second entries appear while the job runs.
redis-cli SLOWLOG RESET
redis-cli SLOWLOG GET 10Confirmed by redis.io
How do I stop it coming back?
- Set socket_timeout explicitly on every redis.Redis() you create, so an upgrade of redis-py cannot change it behind your back.
- Keep blocking pops on a separate worker client, and make their block time shorter than that client's socket_timeout.
- Cap pipeline size in bulk scripts with a constant (for example 10,000 commands) instead of piping a whole dataset at once.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, stegnor.com, Menulo.