STINNER Victor <[email protected]> added the comment:
Here a proof-of-concept of an asynchronous io module reusing the existing
blocking io module: it implements AsyncBufferedReader.readline() using existing
_pyio.BufferedReader.readline().
The approach seems to work, but only if bpo-13322 is fixed first:
BufferedReader, TextIOWrapper & friends must return None if the underlying
object ("raw" and "buffer" objects) return None.
--
My PoC uses 3 classes:
* AsyncFileIO: similar to io.FileIO but uses "async def"
* AsyncBufferedReader: similar to io.BufferedReader but uses "async def"
* FileIOSandwich: glue between asynchronous AsyncFileIO and blocking
io.BufferedReader
At the first read, FileIOSandwich.read(n) call returns None, but it stores the
request read size (n).
If AsyncBufferedReader gets None, is calls FileIOSandwich._prepare_read()
*asynchronously*/
Then FileIOSandwich.read(n) is called again, and this time it no longer blocks,
since data has been already read.
--
Since bpo-13322 is not fixed, my PoC uses _pyio since it's easier to fix. It
needs the following fix for _pyio.BufferedReader.readline():
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index c1bdac7913..e90742ec43 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -557,6 +557,8 @@ class IOBase(metaclass=abc.ABCMeta):
res = bytearray()
while size < 0 or len(res) < size:
b = self.read(nreadahead())
+ if b is None and not res:
+ return None
if not b:
break
res += b
---
Example:
$ ./python poc_aio.py poc_aio.py
data: b'import asyncio\n'
Internally, _prepare_read() reads 8 KiB.
----------
Added file: https://bugs.python.org/file48656/poc_aio.py
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue32561>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com