This is an extension of the
mesh_socket which syncronizes a common
dict. It works by providing an extra handler to store data. This does not expose the entire
dict API, but it exposes a substantial subset, and we’re working to expose more.
This is a fairly inefficient architecture for write intensive applications. For cases where the majority of access is reading, or for small networks, this is ideal. For larger networks where a significant portion of your operations are writing values, you should wait for the chord socket to come into beta.
There are three limitations compared to a normal
- Keys and values can only be
- Keys and values are automatically translated to
- By default, this implements a leasing system which prevents you from changing values set by others for a certain time
You can override the last restriction by constructing with
leasing=False, like so:
>>> from py2p import sync >>> sock = sync.sync_socket('0.0.0.0', 4444, leasing=False)
The only API differences between this and
mesh_socket are for access to this dictionary. They are as follows.
>>> foo = sock.get('test key', None) # Returns None if there is nothing at that key >>> bar = sock[b'test key'] # Raises KeyError if there is nothing at that key >>> assert bar == foo == sock[u'test key'] # Because of the translation mentioned below, these are the same key
It is important to note that keys are all translated to
bytes before being used, so it is required that you use a
bytes-like object. It is also safer to manually convert
unicode keys to
A value can be stored by using the
set() method, or alternately with
__setitem__(). These calls are
O(n), as it has to change values on other nodes. More accurately, the delay between your node knowing of the change and the last node knowing of the change is
>>> sock.set('test key', 'value') >>> sock[b'test key'] = b'value' >>> sock[u'测试'] = 'test'
This will raise a
KeyError if another node has set this value already. Their lease will expire one hour after they set it. If two leases are started at the same UTC second, the tie is settled by doing a string compare of their IDs.
Any node which sets a value can change this value as well. Changing the value renews the lease on it.
Any node which owns a key, can clear its value. Doing this will relinquish your lease on that value. Like the above, this call is
>>> del sock['test']