3 ms·
asyncio by itself doesn't support asynchronous file I/O, see their wiki: https://github.com/python/asyncio/wiki/ThirdParty#filesystem https://github.com/python/
by crvdgc 1y ago
asyncio by itself doesn't support asynchronous file I/O, see their wiki: https://github.com/python/asyncio/wiki/ThirdParty#filesystem https://github.com/python/asyncio/wiki/ThirdParty#filesystem
You have to use something like aiofiles to do that.
- jufter 1y agoInstead of adding another dependency you can just call `loop.run_in_executor` yourself: https://github.com/Tinche/aiofiles/blob/main/src/aiofiles/base.py#L13 https://github.com/Tinche/aiofiles/blob/main/src/aiofiles/ba...
- anordin95 1y agoMhm. You need another thread to accomplish async file reads, which is basically what aiofiles does. This isn't really to the fault of asyncio. The necessary OS primitive isn't available. See the Linux documentation for the O_NONBLOCK flag and note this part: "Note that this flag has no effect for regular files" [1]. I actually originally wrote the sockets example in this article as using file i/o until I came across this bump in the road. [1] https://man7.org/linux/man-pages/man2/open.2.html https://man7.org/linux/man-pages/man2/open.2.html
- jufter 1y agoYep. Even io_uring sends all block device commands to a backend thread pool. I think only benefit is reduced syscall overhead.