Skip to main content

run revert functions when exception occurs in context

Project description

伪事务处理

功能

对于一组函数,如果执行过程中发生异常, 按照反向的调用顺序依次调用对应的逆过程

栗子

from pytransaction import Transaction


def test1(*args, **kwargs):
    print('test1 running..', args, kwargs)


def test2(*args, **kwargs):
    print('test2 running...', args, kwargs)
    raise ValueError('oops, exception occurs')


def rollback(*args, **kwargs):
    print('rolling back...', args, kwargs)


try:
    with Transaction() as t:
        t.run(test1, rollback, args=(1,2), kwargs={'arg3':3}, rargs=('a', 'b'), rkwargs={'t':'tt'})
        t.run(test1, rollback, kwargs={'arg3':3})
        t.run(test1, rollback, args=(4,), kwargs={'a':'aa'}, rargs=(5,6), rkwargs='same')
        t.run(test2, rollback, args=('stop here',), rargs='same')
        t.run(test1, rollback, args=('will not run'))
except Exception as e:
    print('handle exceptions here')
    raise e

注意

因为逆向操作也可能会失败,因此不能完全保证事务一定得到处理

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

pytransaction-1.0.0.tar.gz (1.9 kB view details)

Uploaded Source

File details

Details for the file pytransaction-1.0.0.tar.gz.

File metadata

  • Download URL: pytransaction-1.0.0.tar.gz
  • Upload date:
  • Size: 1.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.13.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for pytransaction-1.0.0.tar.gz
Algorithm Hash digest
SHA256 65fb2f8a9374c9d7a5cf054d18f19e71ae01d3bc0ec2b4cdc9405053d5a4feb0
MD5 5eb9dc268e823e7385f167b0adfb8097
BLAKE2b-256 151b113afb1f5cd0a2f140ea59906fb485fffd4d927da3c683f08065b6533d58

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