A collection of helper functions to implement transactional operations.
Project description
k3txutil
A collection of helper functions to implement transactional operations.
k3txutil is a component of pykit3 project: a python3 toolkit set.
Name
txutil
Status
This library is considered production ready.
Description
A collection of helper functions to implement transactional operations.
Exceptions
CASConflict
syntax:
CASConflict()
User should raise this exception when a CAS conflict detect in a user defined
set
function.
Install
pip install k3txutil
Synopsis
import k3txutil
import threading
class Foo(object):
def __init__(self):
self.lock = threading.RLock()
self.val = 0
self.ver = 0
def _get(self, db, key, **kwargs):
# db, key == 'dbname', 'mykey'
with self.lock:
return self.val, self.ver
def _set(self, db, key, val, prev_stat, **kwargs):
# db, key == 'dbname', 'mykey'
with self.lock:
if prev_stat != self.ver:
raise k3txutil.CASConflict(prev_stat, self.ver)
self.val = val
self.ver += 1
def test_cas(self):
for curr in k3txutil.cas_loop(self._get, self._set, args=('dbname', 'mykey', )):
curr.v += 2
print((self.val, self.ver)) # (2, 1)
while True:
curr_val, stat = getter(key="mykey")
new_val = curr_val + ':foo'
try:
setter(new_val, stat, key="mykey")
except CASConflict:
continue
else:
break
#`cas_loop` simplifies the above workflow to:
for curr in k3txutil.cas_loop(getter, setter, args=("mykey", )):
curr.v += ':foo'
Author
Zhang Yanpo (张炎泼) drdr.xp@gmail.com
Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼) drdr.xp@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file k3txutil-0.1.0.tar.gz
.
File metadata
- Download URL: k3txutil-0.1.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abba256250739bab2c916cfc60cdf5be653db76680a112403b0f173c3e050b2b |
|
MD5 | 798de0e860a9640d82be0484b7aacb0e |
|
BLAKE2b-256 | 9685c6908f14c2fbd0c51b6b6d49fc6dca20576f2c89c3db1260b5dcd802f9c9 |
File details
Details for the file k3txutil-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: k3txutil-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbf83b6b8a0d0edce74230b9cc68bcccef23780b582035378a072d56bc5272ee |
|
MD5 | a84543ae0ef45e5912c5f38dab226a92 |
|
BLAKE2b-256 | 3af0bf7eecdca2df2cc9c47381bdd30f2c8e819738c575f12b540412cdeff845 |