An asyncio example

Adam Bartoš drekin at gmail.com
Fri Jul 3 19:38:07 CEST 2015


Ian Kelly:

>> 2) In the blocked situaction even KeyboardInterrupt doesn't break the
loop,
>> is that desired behavior? And why?
>
> I don't think so. When I tried this locally (using Python 3.4.0, so
> replacing "async def" with "def" and "await" with "yield from" and
> "loop.create_task" with "asyncio.async") pressing Ctrl-C did interrupt
> the loop.
>
Ok, I'll try to get more information and I'll eventually raise an issue.

>> 3) Are there some other issues with my code with respect to “best
practices”
>> how to write a code like this?
>
> There are a couple of approaches you could take. Since your protocol
> is so far text-based, I would suggest adding '\n' to the ends of your
> messages and using reader.readline instead of reader.read.
>
For now, there is no protocol at all. I just wanted to first send all the
data (of arbitrary size) and then wait for whole response. If there is
really no way to close just one direction of a socket, maybe the easiest
way is to have two socket pairs?


Marko Rauhamaa:

>>> 1) is there a way to close just one direction of the connection?
>>
>> No. SOCK_STREAM sockets are always bidirectional.
>
> socket.shutdown(socket.SHUT_WR) does the trick.
>
> I think the asyncio.StreamWriter.write_eof() is the high-level
> equivalent.

You are right that writer.write_eof() behaves like
writer.transport.get_extra_info("socket").shutdown(socket.SHUT_WR) – the
server resumes and sends the response. However, the client still reads
empty bytes afterwards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150703/a7df296b/attachment.html>


More information about the Python-list mailing list