Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93464 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2015-07-03 08:38 -0600 |
| Last post | 2015-07-03 08:38 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: An asyncio example Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-03 08:38 -0600
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-07-03 08:38 -0600 |
| Subject | Re: An asyncio example |
| Message-ID | <mailman.274.1435934355.3674.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web