On Fri, Jul 3, 2015 at 8:31 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Fri, Jul 3, 2015 at 4:28 AM, Adam Bartoš <dre...@gmail.com> wrote: >> Hello, >> >> I'm experimenting with asyncio. I have composed the following code. There is >> a server handler and a client handler. I didn't want to split the code into >> two files so I just used a socketpair, inspired by example >> https://docs.python.org/3/library/asyncio-stream.html#register-an-open-socket-to-wait-for-data-using-streams >> . However, my code doesn't work – it blocks because both sides are trying to >> read more data. But if I close the writer on one side (the commented line), >> whole connection is closed. So >> >> 1) is there a way to close just one direction of the connection? > > No. SOCK_STREAM sockets are always bidirectional. > >> 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. > >> 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. > Alternatively, since the writer isn't planning to write anything > further after its one message, just call writer.write_eof() (not > writer.close) after the call to write.
Sorry, the latter approach doesn't work; it's basically equivalent to calling close. In any case, I suggest using reader.readline. -- https://mail.python.org/mailman/listinfo/python-list