Closed pstch closed 8 years ago
Hi @pstch,
first of all, thanks for helping to make aiofiles better. I agree we should support the new 3.5 protocols, I just haven't gotten around to it yet.
Let me think a little about the nicest way of implementing this.
I think API should be:
async with aiofiles.open('foo.bin', 'wb') as f:
await f.write(b'data')
similar to what aiohttp[1], aiopg[2] and others do. aiofiles.open
should behave like coroutine and async context manager in same time. Example how to do this you could find for instance in aiopg[3](original idea located in aiohttp repository)
[1] http://aiohttp.readthedocs.org/en/stable/client.html#make-a-request [2] https://github.com/aio-libs/aiopg/blob/master/examples/simple_sa.py#L22-L32 [3] https://github.com/aio-libs/aiopg/blob/master/aiopg%2Futils.py#L13
@jettify,
agreed, it'd be best if we can continue mimicking the sync file API. Thanks for the links!
Let me know if you have questions about implementation. But basic idea is following:
1) copy paste that huge context manger object ( it mimics coroutine api)
2) implement custom __aenter__
, __aexit__
3) return context manger object in aiofiles.open
without decorating method with @asyncio.coroutine
Alright, I think I've got this done. Just need to update the tests and docs.
I knew about aiopg's _ContextManager
, but had never used it in my own code and was thus scared to propose it. It seems like a good alternative indeed.
Alright, 0.3.0 commited and pushed to PyPI. Thanks folks!
Also, if this _ContextManager is so useful, why not put it into a nice separate package for everyone to depend on? Would help with code coverage in any case, I don't actually know how to test it all :)
README.md
says :Python 3.5 provides asynchronous context managers and iterators, using
__aenter__
,__aexit__
,__aiter__
and__anext__
. I think adding support for these could provide a nice API for aiofiles, and it wouldn'bt be harder than something along the lines of:The resulting wrapper could now be used as :
My proposed POC has a weird name because I don't really know to integrate it into the class hierarchy, so I made a wrapper that provides the proposed features on top of the objects returned by
aiofiles
coroutines.This is also why I submit this is an issue and not a PR, because I would like to get your opinion on what would be the best way to implement.