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


Groups > comp.lang.python > #93463

Re: An asyncio example

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'handler': 0.04; 'inspired': 0.05; '(using': 0.07; 'blocked': 0.09; 'closed.': 0.09; 'handler.': 0.09; 'loop.': 0.09; 'pressing': 0.09; 'sockets': 0.09; 'python': 0.11; 'suggest': 0.15; '"def"': 0.16; 'adam': 0.16; 'commented': 0.16; 'line),': 0.16; 'sock_stream': 0.16; 'write.': 0.16; 'writer': 0.16; 'wrote:': 0.16; '(not': 0.20; 'trying': 0.22; '(the': 0.22; 'so.': 0.22; 'am,': 0.23; 'code.': 0.23; '2015': 0.23; 'replacing': 0.23; 'split': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.25; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; "i'm": 0.29; 'loop,': 0.29; 'connection': 0.30; 'fri,': 0.31; 'code': 0.31; 'url:python': 0.33; 'this?': 0.34; 'server': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'direction': 0.35; 'protocol': 0.35; "isn't": 0.35; 'but': 0.36; 'url:org': 0.36; 'there': 0.36; 'data.': 0.36; 'url:library': 0.36; 'two': 0.37; 'client': 0.37; "didn't": 0.37; 'subject:: ': 0.37; 'desired': 0.37; 'instead': 0.38; 'files': 0.38; 'url:docs': 0.39; 'to:addr:python.org': 0.39; 'hello,': 0.40; 'some': 0.40; 'further': 0.60; 'url:3': 0.60; 'your': 0.60; 'even': 0.61; 'close': 0.61; 'side': 0.62; 'more': 0.62; 'respect': 0.65; 'approaches': 0.72; 'jul': 0.72; '\xe2\x80\x93': 0.77; '"yield': 0.84; 'from"': 0.84; 'to:name:python': 0.84; 'interrupt': 0.91; 'why?': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=0qo3jneLeLZnOWq9WvhIp/mcsCWFCJ0EHU6cvlaCMZg=; b=kkhuCpHkLOWhGDHDJl2EOwaD3diaxJ3xh2A8VUSDN1XpPMa1wL+JKRMkHQSbteWJSY yo4LIU63LVqFA7QqaylrKdjPj7p+7039vedj38463QK0bBeikAxm8XsXK/U2ncu9ceaI 9FXD7O2Nk9ZkLDTZu1ibb7lHhHGp0sn9zetaD6IAEhZtYPauOJIHiZR4ZE+gLADCuNXN d738BFAFk5nI7bUddlYUT5mFV0NQ3rmGbBVTIohmzjVpqtPRty9KSvygXWFm5QgoFndF wNmE+97XIfYtjHVfUBrcrk2HI3fySmQhViDiF1QboCCmwuV5FD/9g56SRq4wwcwJCxjn Le8w==
X-Received by 10.170.129.84 with SMTP id v81mr44707315ykb.124.1435933915328; Fri, 03 Jul 2015 07:31:55 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CACvLUangsktYPpG2gkW6yr-3zq+=t-Nkfefo6tnOU4bQX2Q++A@mail.gmail.com>
References <CACvLUangsktYPpG2gkW6yr-3zq+=t-Nkfefo6tnOU4bQX2Q++A@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 3 Jul 2015 08:31:15 -0600
Subject Re: An asyncio example
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.273.1435933923.3674.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1435933923 news.xs4all.nl 2948 [2001:888:2000:d::a6]:56791
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:93463

Show key headers only | View raw


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.

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


Thread

Re: An asyncio example Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-03 08:31 -0600
  Re: An asyncio example Marko Rauhamaa <marko@pacujo.net> - 2015-07-03 18:14 +0300
    Re: An asyncio example Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-03 23:58 -0600

csiph-web