-
Notifications
You must be signed in to change notification settings - Fork 2
Optimise reading data from socket #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import asyncio | ||
|
||
|
||
async def open_connection( | ||
host=None, port=None, *, loop=None, limit=2 ** 16, **kwargs): | ||
|
||
if loop is None: | ||
loop = asyncio.get_event_loop() | ||
reader = StreamReader(limit=limit, loop=loop) | ||
protocol = asyncio.StreamReaderProtocol(reader, loop=loop) | ||
transport, _ = await loop.create_connection( | ||
lambda: protocol, host, port, **kwargs) | ||
writer = asyncio.StreamWriter(transport, protocol, reader, loop) | ||
return reader, writer | ||
|
||
|
||
class StreamReader(asyncio.StreamReader): | ||
def is_data_available(self, n): | ||
""" | ||
Check if there is n length data in the buffer | ||
""" | ||
if len(self._buffer) >= n: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple of comments here. The final implementation of the Second, but TBH taking into account that this is a POC is not a stopper for me is the accessing of an internal attribute like [1] https://github.com/python/cpython/blob/master/Lib/asyncio/streams.py#L1006 |
||
return True | ||
|
||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a
coro
and a coro does not implement the Future interface, indeed the coro won't be scheduled in that scenario, not even started.My suggestion is to implement a new method in the
StreamReader
for reading without having to wait and then if you want to wrap the result in aFuture
this will allow you to reuse the_read_cb
function, for example