[Async-sig] async contextmanager

Герасимов Михаил Gerasimov-M-N at yandex.ru
Tue Nov 1 13:23:51 EDT 2016


I played with it some time ago. Actually, original code 
<https://github.com/python/cpython/blob/master/Lib/contextlib.py> with 
few changes works fine.

Here's example:

https://gist.github.com/germn/6b2f8a1afbfc322daa01365cacd1d9ad

Test it:

    import asyncio
    from acontextlib import acontextmanager


    state = []

    @acontextmanager
    async def woohoo():
         state.append(1)
         yield 42
         state.append(999)


    async def main():
         async with woohoo() as x:
             assert state == [1]
             assert x == 42


    if __name__ == '__main__':
         loop = asyncio.get_event_loop()
         loop.run_until_complete(main())




01.11.2016 19:15, Federico Marani пишет:
> Has anyone tried to build an asynchronous version of @contextmanager? 
> Is it even possible... given the use of yield?
>
>
> _______________________________________________
> Async-sig mailing list
> Async-sig at python.org
> https://mail.python.org/mailman/listinfo/async-sig
> Code of Conduct: https://www.python.org/psf/codeofconduct/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20161101/4c4a61b4/attachment.html>


More information about the Async-sig mailing list