使用contextmanager捕获指令以供以后执行

时间:2019-03-06 20:03:57

标签: python transactions contextmanager

我想使用上下文管理器实现类似伪db的事务。

例如:

   AddColumn("dbo.ChildClass", "ChildField", c => c.String());
   AddColumn("dbo.ChildClass", "BaseField", c => c.String());

如果发生异常(例如异常),请丢弃class Transactor: def a(): pass def b(d, b): pass def c(i): pass @contextmanager def get_session(self): txs = [] yield self # accumulate method calls for tx in tx: tx() # somehow pass the arguments def main(): t = Transactor() with t.get_session() as session: session.a() # inserts `a` into `txs` ... more code ... session.c(value) # inserts `c` and `(value)` into `txs` session.b(value1, value2) # inserts `b` and `(value1, value2)` into `txs` ... more code ... # non-transator related code f = open('file.txt') # If this throws an exception, # break out of the context manager, # and discard previous transactor calls. ... more code ... session.a() # inserts `a` into `txs` session.b(x, y) # inserts `b` and `(x, y)` into `txs` # Now is outside of context manager. # The following calls should execute immediately t.a() t.b(x, y) t.c(k) (回滚)。如果到达上下文末尾,请按插入顺序执行每条指令,并传递适当的参数。

如何捕获方法调用以供以后执行?

还有一个警告: 如果未调用txs,我想立即执行指令。

1 个答案:

答案 0 :(得分:1)

这不是很漂亮,但是要遵循您正在寻找的结构,您必须构建一个临时事务类来保存您的函数队列,并在上下文管理器退出后执行它。您需要使用functools.partial,但是有一些限制:

  1. 所有排队的呼叫必须是基于“会话”实例的方法。任何其他事情都会立即执行。
  2. 我不知道您要如何处理不可调用的会话属性,所以现在我假设它只是检索值。

话虽如此,这是我的看法:

from functools import partial

class TempTrans:

    # pass in the object instance to mimic
    def __init__(self, obj):
        self._queue = []

        # iterate through the attributes and methods within the object and its class
        for attr, val in type(obj).__dict__.items() ^ obj.__dict__.items():
            if not attr.startswith('_'):
                if callable(val):
                    setattr(self, attr, partial(self._add, getattr(obj, attr)))
                else:
                    # placeholder to handle non-callable attributes
                    setattr(self, attr, val)

    # function to add to queue
    def _add(self, func, *args, **kwargs):
        self._queue.append(partial(func, *args, **kwargs))

    # function to execute the queue
    def _execute(self):
        _remove = []

        # iterate through the queue to call the functions.  
        # I suggest catching errors here in case your functions falls through
        for func in self._queue:
            try:
                func()
                _remove.append(func)
            except Exception as e:
                print('some error occured')
                break

        # remove the functions that were successfully ran
        for func in _remove:
            self._queue.remove(func)

现在进入上下文管理器(它将在您的类之外,如果需要,您可以将其作为类方法放置):

@contextmanager
def temp_session(obj):
    t = TempTrans(obj)
    try:
        yield t
        t._execute()
        print('Transactions successfully ran')
    except:
        print('Encountered errors, queue was not executed')
    finally:
        print(t._queue)  # debug to see what's left of the queue

用法:

f = Foo()

with temp_session(f) as session:
    session.a('hello')
    session.b(1, 2, 3)

# a hello
# b 1 2 3
# Transactions successfully ran
# []

with temp_session(f) as session:
    session.a('hello')
    session.b(1, 2, 3)
    session.attrdoesnotexist  # expect an error

# Encountered errors, queue was not executed
# [
#   functools.partial(<bound method Foo.a of <__main__.Foo object at 0x0417D3B0>>, 'hello'), 
#   functools.partial(<bound method Foo.b of <__main__.Foo object at 0x0417D3B0>>, 1, 2, 3)
# ]

此解决方案由于您希望其结构化的方式而有些许设计,但是如果您不需要上下文管理器并且不需要 会话,看起来像一个直接函数调用,仅使用partial是很简单的:

my_queue = []

# some session
my_queue.append(partial(f, a))
my_queue.append(partial(f, b))
for func in my_queue:
    func()