Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*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; 'basically': 0.18; '(not': 0.20; 'trying': 0.22; '(the': 0.22; 'so.': 0.22; 'latter': 0.22; 'sorry,': 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; 'equivalent': 0.27; '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; 'case,': 0.34; '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=iaSJtbEmSrDptOtxvOY461SBoIc4rt942e3SLUlbvOs=; b=IVfUoTIUgIVwmphDbhSVzkDecLe4OuVfOdDTvx0i9P+fStEXX+6AA1GT0llCdyi8nF TtL9L2WK2UjJXNF9gtydbu7zZNYbjUsiAkVNT0KEd4UCG1G1HW0oo63RqfG4erybsO81 0chP7dJ1fMFPvRNYDCBwfjedyd4cSY/zPswi+9eEPL1WRem+6qjWAYeG7hx3D9z9AQcd xUZHHRHsGyBnWloIzBGQKIwoP2DASVr5tEPvYn8d3WJtPBkM2nTBRH+CTcdvhD39MrkX 6lVTCLVghQQpVlx0cOJoLG7aU+RuTLGygvH6CY0S6KLGQwDJ7Q/Ag4bzhLdaCWLdA+as jXCw== X-Received: by 10.129.56.70 with SMTP id f67mr44524191ywa.85.1435934346178; Fri, 03 Jul 2015 07:39:06 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Fri, 3 Jul 2015 08:38:26 -0600 Subject: Re: An asyncio example To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435934355 news.xs4all.nl 2910 [2001:888:2000:d::a6]:59775 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93464 On Fri, Jul 3, 2015 at 8:31 AM, Ian Kelly wrote: > On Fri, Jul 3, 2015 at 4:28 AM, Adam Barto=C5=A1 wrote= : >> Hello, >> >> I'm experimenting with asyncio. I have composed the following code. Ther= e is >> a server handler and a client handler. I didn't want to split the code i= nto >> two files so I just used a socketpair, inspired by example >> https://docs.python.org/3/library/asyncio-stream.html#register-an-open-s= ocket-to-wait-for-data-using-streams >> . However, my code doesn't work =E2=80=93 it blocks because both sides a= re trying to >> read more data. But if I close the writer on one side (the commented lin= e), >> 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 lo= op, >> 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 =E2=80=9Cbes= t practices=E2=80=9D >> 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.