Skip to content

Commit

Permalink
Add context manager section
Browse files Browse the repository at this point in the history
Also realized that the __init__ section is not pubished in sphinx,
instead the class docstring is treated as the initialzier docstring.
So, future work to sift through and move them all.
  • Loading branch information
stealthycoin committed Dec 12, 2018
1 parent 3785698 commit 1a159be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ More examples can be found in the
`examples <https://github.com/stealthycoin/lynk/tree/master/examples>`_
directory in the source repo.


Context manager
---------------

In the above example we manually call ``acquire()`` and ``release()``. This depends on no
exceptions ocurring, and would generally be safer in a ``try: finally:`` block. For
convenience the :class:`lynk.lock.Lock` object can be called and used as a context manager.
The following code:

.. code-block:: python
lock.acquire()
time.sleep(10)
lock.release()
Can be re-written more safely, and conveinently, as:

.. code-block:: python
with lock():
time.sleep(10)
This ensures the releasing in the lock in the case of an unexpected exception.


Teardown
--------

Expand Down
44 changes: 24 additions & 20 deletions src/lynk/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,32 @@ def _stop_refresher(self):


class LockFactory(object):
"""Class for constructing locks."""
"""Class for creating locks.
A ``LockFactory`` represents the logical binding beteween an agent that
can interact with locks, and a backend table where the locks are stored.
This relationship is defined by the ``table_name`` and ``host_identifier``
parameters outlined below.
:type table_name: str
:param table_name: Name of the table in the backend.
:type host_identifier: str
:param host_identifier: A unique identifier for a host. A host is just
an unused field in the database. It is for debugging and nothing
more so any value can be used that has meaning to the developer.
By default hostname is used.
:type backend_bridge_factory: Anything with a create method or None.
:param backend_bridge_factory: A factory that creates our backend and
its associated bridge class to be injected into our lock. Usually
these need to be created by a shared factory class because they
have shared dependencies. If None is provided the default is a
:class:`lynk.backends.dynamodb.DynamoDBBackendBridgeFactory` which
will create locks bound to a DynamoDB Table.
"""
def __init__(self, table_name, host_identifier=None,
backend_bridge_factory=None):
"""Initialize a new LockFactory.
:type table_name: str
:param table_name: Name of the table in the backend.
:type host_identifier: str
:param host_identifier: A unique identifier for a host. A host is just
an unused field in the database. It is for debugging and nothing
more so any value can be used that has meaning to the developer.
By default hostname is used.
:type backend_bridge_factory: Anything with a create method or None.
:param backend_bridge_factory: A factory that creates our backend and
its associated bridge class to be injected into our lock. Usually
these need to be created by a shared factory class because they
have shared dependencies. If None is provided the default is a
:class:`lynk.backends.dynamodb.DynamoDBBackendBridgeFactory` which
will create locks bound to a DynamoDB Table.
"""
self._table_name = table_name
if host_identifier is None:
host_identifier = socket.gethostname()
Expand Down

0 comments on commit 1a159be

Please sign in to comment.