Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93464

Re: An asyncio example

References <CACvLUangsktYPpG2gkW6yr-3zq+=t-Nkfefo6tnOU4bQX2Q++A@mail.gmail.com> <CALwzidmYU9N-FK1frn54dS=V2pkSt=2hYcru7t3RpQTc=c8CGA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-07-03 08:38 -0600
Subject Re: An asyncio example
Newsgroups comp.lang.python
Message-ID <mailman.274.1435934355.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jul 3, 2015 at 8:31 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Jul 3, 2015 at 4:28 AM, Adam Bartoš <drekin@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: An asyncio example Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-03 08:38 -0600

csiph-web