File: api_documentation.rst

package info (click to toggle)
python-sherlock 0.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: python: 2,008; makefile: 162
file content (280 lines) | stat: -rw-r--r-- 9,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
.. :mod:`sherlock` documentation master file, created by
   sphinx-quickstart on Wed Jan 22 11:28:21 2014.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

API Documentation
=================

Configuration
+++++++++++++

:mod:`sherlock` can be globally configured to set a lot of defaults using the
:func:`sherlock.configure` function. The configuration set using
:func:`sherlock.configure` will be used as the default for all the lock
objects.

.. autofunction:: sherlock.configure

Understanding the configuration parameters
------------------------------------------

:func:`sherlock.configure` accepts certain configuration patterns which are
individually explained in this section. Some of these configurations are global
configurations and cannot be overriden while others can be overriden at
individual lock levels.

``backend``
~~~~~~~~~~~

The `backend` parameter allows you to set which backend you would like to use
with the :class:`sherlock.Lock` class. When set to a particular backend,
instances of :class:`sherlock.Lock` will use this backend for lock
synchronization.

Basic Usage:

>>> import sherlock
>>> sherlock.configure(backend=sherlock.backends.REDIS)

.. note:: this configuration cannot be overriden at the time of creating a
          class object.

Available Backends
""""""""""""""""""

To set the `backend` global configuration, you would have to choose one from
the defined backends. The defined backends are:

* Etcd: :attr:`sherlock.backends.ETCD`
* Memcache: :attr:`sherlock.backends.MEMCACHE`
* Redis: :attr:`sherlock.backends.REDIS`

``client``
~~~~~~~~~~

The `client` parameter allows you to set a custom clien object which
:mod:`sherlock` can use for connecting to the backend store. This gives you the
flexibility to connect to the backend store from the client the way you want.
The provided custom client object must be a valid client object of the
supported client libraries. If the global `backend` has been set, then the
provided custom client object must be an instance of the client library
supported by that backend. If the backend has not been set, then the custom
client object must be an instance of a valid supported client. In this case,
:mod:`sherlock` will set the backend by instrospecting the type of the provided
client object.

The global default client object set using the `client` parameter will be used
only by :class:`sherlock.Lock` instances. Other :ref:`backend-specific-locks`
will either use the provided client object at the time of instantiating the
lock object of their types or will default to creating a simple client object
by themselves for their backend store, which will assume that their backend
store is running on localhost.

.. note:: this configuration cannot be overriden at the time of creating a
          class object.

Example:

>>> import redis
>>> import sherlock
>>>
>>> # Configure just the backend
>>> sherlock.configure(backend=sherlock.backends.REDIS)
>>>
>>> # Configure the global client object. This sets the client for all the
>>> # locks.
>>> sherlock.configure(client=redis.StrictRedis())

And when the provided client object does not match the supported library for
the set backend:

>>> import etcd
>>> import sherlock
>>>
>>> sherlock.configure(backend=sherlock.backends.REDIS)
>>> sherlock.configure(client=etcd.Client())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/vkapoor/Development/sherlock/sherlock/__init__.py", line 148, in configure
    _configuration.update(**kwargs)
  File "/Users/vkapoor/Development/sherlock/sherlock/__init__.py", line 250, in update
    setattr(self, key, val)
  File "/Users/vkapoor/Development/sherlock/sherlock/__init__.py", line 214, in client
    self.backend['name']))
ValueError: Only a client of the redis library can be used when using REDIS as the backend store option.

And when the backend is not configured:

>>> import redis
>>> import sherlock
>>>
>>> # Congiure just the client, this will configure the backend to
>>> # sherlock.backends.REDIS automatically.
>>> sherlock.configure(client=redis.StrictRedis())

And when the client object passed as argument for client parameter is not a
valid client object at all:

>>> import sherlock
>>> sherlock.configure(client=object())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/vkapoor/Development/sherlock/sherlock/__init__.py", line 148, in configure
    _configuration.update(**kwargs)
  File "/Users/vkapoor/Development/sherlock/sherlock/__init__.py", line 250, in update
    setattr(self, key, val)
  File "/Users/vkapoor/Development/sherlock/sherlock/__init__.py", line 221, in client
    raise ValueError('The provided object is not a valid client'
ValueError: The provided object is not a valid client object. Client objects can only be instances of redis library's client class, python-etcd library's client class or pylibmc library's client class.

``namespace``
~~~~~~~~~~~~~

The ``namespace`` parameter allows you to configure :mod:`sherlock` to set keys
for synchronizing locks with a namespace so that if you are using the same
datastore for something else, the keys set by locks don't conflict with your
other keys set by your application.

In case of Redis and Memcached, the name of your locks are prepended with
``NAMESPACE_`` (where ``NAMESPACE`` is the namespace set by you). In case of
Etcd, a directory with the name same as the ``NAMESPACE`` you provided is
created and the locks are created in that directory.

By default, :mod:`sherlock` does not namespace the keys set for locks.

.. note:: this configuration can be overriden at the time of creating a
          class object.

``expire``
~~~~~~~~~~

This parameter can be used to set the expiry of locks. When set to :obj:`None`,
the locks will never expire.

This parameter's value defaults to ``60 seconds``.

.. note:: this configuration can be overriden at the time of creating a
          class object.

Example:

>>> import sherlock
>>> import time
>>>
>>> # Configure locks to expire after 2 seconds
>>> sherlock.configure(expire=2)
>>>
>>> lock = sherlock.Lock('my_lock')
>>>
>>> # Acquire the lock
>>> lock.acquire()
True
>>>
>>> # Sleep for 2 seconds to let the lock expire
>>> time.sleep(2)
>>>
>>> # Acquire the lock
>>> lock.acquire()
True

``timeout``
~~~~~~~~~~~

This parameter can be used to set after how much time should :mod:`sherlock`
stop trying to acquire an already acquired lock.

This parameter's value defaults to ``10 seconds``.

.. note:: this configuration can be overriden at the time of creating a
          class object.

Example:

>>> import sherlock
>>>
>>> # Configure locks to timeout after 2 seconds while trying to acquire an
>>> # already acquired lock
>>> sherlock.configure(timeout=2, expire=10)
>>>
>>> lock = sherlock.Lock('my_lock')
>>>
>>> # Acquire the lock
>>> lock.acquire()
True
>>>
>>> # Acquire the lock again and let the timeout elapse
>>> lock.acquire()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sherlock/lock.py", line 170, in acquire
    'lock.' % self.timeout)
sherlock.lock.LockTimeoutException: Timeout elapsed after 2 seconds while trying to acquiring lock.

``retry_interval``
~~~~~~~~~~~~~~~~~~

This parameter can be used to set after how much time should :mod:`sherlock`
try to acquire lock after failing to acquire it. For example, a log has already
been acquired, then when another lock object tries to acquire the same lock,
it fails. This parameter sets the time interval for which we should sleep
before retrying to acquire the lock.

This parameter can be set to 0 to continuously try acquiring the lock. But that
will also mean that you are bombarding your datastore with requests one after
another.

This parameter's value defaults to ``0.1 seconds (100 milliseconds)``.

.. note:: this configuration can be overriden at the time of creating a
          class object.

Generic Locks
+++++++++++++

:mod:`sherlock` provides generic locks that can be globally configured to use a
specific backend, so that most of your application code does not have to care
about which backend you are using for lock synchronization and makes it easy
to change backend without changing a ton of code.

.. autoclass:: sherlock.Lock
    :members:
    :inherited-members:

.. _backend-specific-locks:

Backend Specific Locks
++++++++++++++++++++++

:mod:`sherlock` provides backend specific Lock classes as well which can be optionally
used to use different backend than a globally configured backend. These locks
have the same interface and semantics as :class:`sherlock.Lock`.

Redis based Locks
-----------------

.. autoclass:: sherlock.RedisLock
    :members:
    :inherited-members:

Etcd based Locks
----------------

.. autoclass:: sherlock.EtcdLock
    :members:
    :inherited-members:

Memcached based Locks
---------------------

.. autoclass:: sherlock.MCLock
    :members:
    :inherited-members:

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`