def future_callback(future): assert future.result() == 'Time passed' # this is no man's land, no transaction here trans = transaction.get(None, loop=event_loop) assert trans is None # but here there's a way to get one because the future was added # to the transaction trans = transaction.get(None, loop=event_loop, task=future) assert trans is not None and trans.parent is master_trans with trans: func_called_by_callback()
def sync(): trans = transaction.get(None, loop=event_loop) assert trans is not None fut = asyncio.Future(loop=event_loop) # there's no way to have some control on the order callbacks are # called so i we want to have a callback covered by the transaction, # we must add it to the transaction _after_ the callback fut.add_done_callback(future_callback) trans.add(fut) event_loop.call_later(0.5, future_simulated_end, fut)
def non_coro_func(): tran = transaction.get() c = stashed_coro() tran.add(c)
def non_coro_func(): tran = transaction.get(loop=event_loop) c = stashed_coro() tran.add(c)
def func_called_by_callback(): nonlocal end trans = transaction.get(None, loop=event_loop) assert trans is not None and trans.parent is master_trans end = 'done!'
def future_simulated_end(future): # this is no man's land, no transaction here trans = transaction.get(None, loop=event_loop) assert trans is None future.set_result('Time passed')
def coro2(): trans = transaction.get(None) assert trans is not None return 2
def sync_func(): trans = transaction.get(None, loop=event_loop) assert trans is outer_trans another = asyncio.ensure_future(on_another_task()) log.append('on sync') trans.add(another)
def coro1(): assert event_loop is asyncio.get_event_loop() trans = transaction.get(None) assert trans is not None return 1
def on_another_task(): log.append('on async') trans = transaction.get(None) assert trans.parent is outer_trans
def __init__(self): tran = transaction.get(loop=event_loop) c = stashed_coro() tran.add(c, cback=self._init)