Hi,

I added 3 examples to show to to register an open sockets in asyncio
documentation:

add_register() with reader callback:
https://docs.python.org/dev/library/asyncio-eventloop.html#asyncio-watch-read-event

Protocol:
https://docs.python.org/dev/library/asyncio-protocol.html#asyncio-register-socket

Stream:
https://docs.python.org/dev/library/asyncio-stream.html#register-an-open-socket-to-wait-for-data-using-streams

I would like to ensure that all sockets are closed at the end of the
example. For streams, I call writer.close(), but I don't see how to
wait until the socket is closed. It doesn't look to be implemented
directly. It looks like currently, it's only possible to call read()
in a loop until read() returns an empty string:

@asyncio.coroutine
def  wait_eof(reader):
    while True:
        # Don't call read() becaues it may allocate a large buffer
        data = yield from reader.read(4096)
        if data:
            break

Is there another way to wait until the socket is closed? My wait_eof()
looks complex, I don't want to put it in a simple example.

Or maybe I should not wait until a socket is closed? Is it safe to
call writer.close() and exit immediatly? The scheduled callback which
will close the socket may not be called immediatly.

Victor

Reply via email to