Configuration Parsing

baseplate.context.redis.pool_from_config(app_config, prefix=u'redis.', **kwargs)

Make a ConnectionPool from a configuration dictionary.

The keys useful to pool_from_config() should be prefixed, e.g. redis.url, redis.max_connections, etc. The prefix argument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on the redis.ConnectionPool constructor.

Supported keys:

  • url (required): a URL like redis://localhost/0.
  • max_connections: an integer maximum number of connections in the pool
  • socket_connect_timeout: a timespan of how long to wait for sockets
    to connect. e.g. 200 milliseconds.
  • socket_timeout: a timespan of how long to wait for socket operations,
    e.g. 200 milliseconds.


class baseplate.context.redis.RedisContextFactory(connection_pool)

Redis client context factory.

This factory will attach a MonitoredRedisConnection to an attribute on the context object. When redis commands are executed via this connection object, they will use connections from the provided redis.ConnectionPool and automatically record diagnostic information.

Parameters:connection_pool (redis.ConnectionPool) – A connection pool.
class baseplate.context.redis.MonitoredRedisConnection(context_name, server_span, connection_pool)

Redis connection that collects diagnostic information.

This connection acts like redis.StrictRedis except that all operations are automatically wrapped with diagnostic collection.

The interface is the same as that class except for the pipeline() method.


Locks and PubSub are currently unsupported.

execute_command(command, *args, **kwargs)

Execute a command and return a parsed response

pipeline(name, transaction=True, shard_hint=None)

Create a pipeline.

This returns an object on which you can call the standard redis commands. Execution will be deferred until execute is called. This is useful for saving round trips.

  • name (str) – The name to attach to diagnostics for this pipeline.
  • transaction (bool) – Whether or not the commands in the pipeline are wrapped with a transaction and executed atomically.
transaction(*args, **kwargs)

Convenience method for executing the callable func as a transaction while watching all keys specified in watches. The ‘func’ callable should expect a single argument which is a Pipeline object.

lock(*args, **kwargs)

Return a new Lock object using key name that mimics the behavior of threading.Lock.

If specified, timeout indicates a maximum life for the lock. By default, it will remain locked until release() is called.

sleep indicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock.

blocking_timeout indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of None indicates continue trying forever. blocking_timeout can be specified as a float or integer, both representing the number of seconds to wait.

lock_class forces the specified lock implementation.

thread_local indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline:

time: 0, thread-1 acquires my-lock, with a timeout of 5 seconds.
thread-1 sets the token to “abc”
time: 1, thread-2 blocks trying to acquire my-lock using the
Lock instance.
time: 5, thread-1 has not yet completed. redis expires the lock
time: 5, thread-2 acquired my-lock now that it’s available.
thread-2 sets the token to “xyz”
time: 6, thread-1 finishes its work and calls release(). if the
token is not stored in thread local storage, then thread-1 would see the token value as “xyz” and would be able to successfully release the thread-2’s lock.

In some use cases it’s necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn’t disabled in this case, the worker thread won’t see the token set by the thread that acquired the lock. Our assumption is that these cases aren’t common and as such default to using thread local storage.

pubsub(*args, **kwargs)

Return a Publish/Subscribe object. With this object, you can subscribe to channels and listen for messages that get published to them.

class baseplate.context.redis.MessageQueue(name, client)

A Redis-backed variant of MessageQueue.

  • name – can be any string.
  • client – should be a redis.ConnectionPool or redis.BlockingConnectionPool from which a client connection can be created from (preferably generated from the pool_from_config() helper).

Read a message from the queue.

Parameters:timeout (int) – If the queue is empty, the call will block up to timeout seconds or forever if None, if a float is given, it will be rounded up to be an integer
Raises:TimedOutError The queue was empty for the allowed duration of the call.
put(message, timeout=None)

Add a message to the queue.

Parameters:message – will be typecast to a string upon storage and will come out of the queue as a string regardless of what type they are when passed into this method.

Not implemented for Redis variant


Close queue when finished

Will delete the queue from the Redis server (Note, can still enqueue and dequeue as the actions will recreate the queue)