def middleware(request): local = muffin.local(app.loop) user = yield from app.ps.session.load_user(request) local.current_user = user response = yield from handler(request) local.current_user = None return response
async def test_local(loop): l1 = muffin.local(loop) l2 = muffin.local(loop) assert l1 is l2 log, fut1, fut2 = [], asyncio.Future(), asyncio.Future() async def coro1(): l1.value = 'task1' await fut1 log.append(l1.value) fut2.set_result(True) async def coro2(): l1.value = 'task2' fut1.set_result(True) await fut2 log.append(l1.value) await asyncio.wait([coro1(), coro2()]) assert log == ['task1', 'task2']
def test_local(loop): l1 = muffin.local(loop) l2 = muffin.local(loop) assert l1 is l2 log, fut1, fut2 = [], asyncio.Future(), asyncio.Future() @asyncio.coroutine def coro1(): l1.value = 'task1' yield from fut1 log.append(l1.value) fut2.set_result(True) @asyncio.coroutine def coro2(): l1.value = 'task2' fut1.set_result(True) yield from fut2 log.append(l1.value) yield from asyncio.wait([coro1(), coro2()]) assert log == ['task1', 'task2']
def current_user_context(): local = muffin.local(app.loop) current_user = getattr(local, 'current_user') return {'user': current_user}