Skip to main content

Compare-and-swap (CAS) loop utilities for transactional operations

Project description

k3txutil

Action-CI Build Status Documentation Status Package

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k3txutil-0.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

k3txutil-0.1.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file k3txutil-0.1.1.tar.gz.

File metadata

  • Download URL: k3txutil-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3txutil-0.1.1.tar.gz
Algorithm Hash digest
SHA256 88b781853093fa10039943654cb81612517407c4eda23eb7c85e1c59e8460363
MD5 e29d223662c9f61c4d2a8e25e1162dce
BLAKE2b-256 40331cfe72d1a33a58d2079a29cbfd8a27cc9e62ed51748fc469f90d46e70b55

See more details on using hashes here.

File details

Details for the file k3txutil-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: k3txutil-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3txutil-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b54238f6430897baff231bd642513f05f85bab9ce0b4effb2c30d36fc9c223e
MD5 4a4490a75dcda16aa2a2a4ffa79620bc
BLAKE2b-256 d4362b53c28329d691c6071fb639959e846d5c83015ec8c99e35e329a605dc66

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page