Stash

Object for storing cross-request state. This is unusual in that keys must be UUIDs, in order to prevent different clients setting the same key, and values are write-once, read-once to minimize the chances of state persisting indefinitely. The stash defines two operations; put, to add state and take to remove state. Furthermore, the view of the stash is path-specific; by default a request will only see the part of the stash corresponding to its own path.

A typical example of using a stash to store state might be:

@handler
def handler(request, response):
    # We assume this is a string representing a UUID
    key = request.GET.first("id")

    if request.method == "POST":
        request.server.stash.put(key, "Some sample value")
        return "Added value to stash"
    else:
        value = request.server.stash.take(key)
        assert request.server.stash.take(key) is None
        return value

Interface

class wptserve.stash.QueueProxy(token, serializer, manager=None, authkey=None, exposed=None, incref=True, manager_owned=False)
class wptserve.stash.Stash(default_path, address=None, authkey=None)

Key-value store for persisting data across HTTP/S and WS/S requests.

This data store is specifically designed for persisting data across server requests. The synchronization is achieved by using the BaseManager from the multiprocessing module so different processes can acccess the same data.

Stash can be used interchangeably between HTTP, HTTPS, WS and WSS servers. A thing to note about WS/S servers is that they require additional steps in the handlers for accessing the same underlying shared data in the Stash. This can usually be achieved by using load_env_config(). When using Stash interchangeably between HTTP/S and WS/S request, the path part of the key should be expliclitly specified if accessing the same key/value subset.

The store has several unusual properties. Keys are of the form (path, uuid), where path is, by default, the path in the HTTP request and uuid is a unique id. In addition, the store is write-once, read-once, i.e. the value associated with a particular key cannot be changed once written and the read operation (called “take”) is destructive. Taken together, these properties make it difficult for data to accidentally leak between different resources or different requests for the same resource.

put(key, value, path=None)

Place a value in the shared stash.

Parameters:
  • key – A UUID to use as the data’s key.

  • value – The data to store. This can be any python object.

  • path – The path that has access to read the data (by default the current request path)

take(key, path=None)

Remove a value from the shared stash and return it.

Parameters:
  • key – A UUID to use as the data’s key.

  • path – The path that has access to read the data (by default the current request path)

exception wptserve.stash.StashError
class wptserve.stash.StashManager(address=None, authkey=None, serializer='pickle', ctx=None, *, shutdown_timeout=1.0)