Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'socket': 0.07; 'high- level': 0.09; 'higher-level': 0.09; 'sockets': 0.09; 'underlying': 0.09; 'bug': 0.10; 'def': 0.14; 'connect()': 0.16; 'empty.': 0.16; 'sock_stream': 0.16; 'written.': 0.16; 'wrote:': 0.16; 'skip:{ 20': 0.18; '>>>': 0.20; 'am,': 0.23; '2015': 0.23; 'replacing': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'wondering': 0.25; 'said,': 0.27; 'yield': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; "i'm": 0.29; 'equivalent.': 0.29; 'correct': 0.29; 'fri,': 0.31; 'skip:s 30': 0.31; 'code': 0.31; 'url:python': 0.33; 'server': 0.34; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'direction': 0.35; 'url:org': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'does': 0.39; 'to:addr:python.org': 0.39; 'seem': 0.39; 'called': 0.40; 'back': 0.61; 'close': 0.61; 'skip:a 40': 0.64; 'stand': 0.64; 'jul': 0.72; "op's": 0.84; 'to:name:python': 0.84; 'trick.': 0.84; 'url:cpython': 0.84 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; bh=4YlXj1iugjCAzNYbVf4yFfXN+lxVvMu1vsiuUtnRat0=; b=whOcK2a3DTNnnutS0OpdAo2QLEZmqrRXiqFDwIN8yH34FmZrW2J4gXMDkUhfMp3iGl GL0PkwsWzZmi0N2AcrMnmXSgLbWFb1w5FdnsLx2taa4Kz1AZT5YliBYP3GaE9nA4gSoT rXxrbq2Ugf6/XvHSrEO4Kr8jURDb4OFROaBYD642fbJcMgzkohi6yh/lzxOEwQ4U0icV 4iezguOMLIVVzvJaWVmcHOipeaMtRpgkNGp0iR6IdKJEajkOPORkYuvzqehaGP0ytiai YvVfvwKm5hkAGAwcgwF7EaUWXlQbGy/6PrtQXzLWG+d4zqQq0KmwASNtCMHpwRCSWga6 EWlQ== X-Received: by 10.129.41.18 with SMTP id p18mr48648284ywp.147.1435989564710; Fri, 03 Jul 2015 22:59:24 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87vbe1qnn6.fsf@elektro.pacujo.net> References: <87vbe1qnn6.fsf@elektro.pacujo.net> From: Ian Kelly Date: Fri, 3 Jul 2015 23:58:45 -0600 Subject: Re: An asyncio example To: Python Content-Type: text/plain; charset=UTF-8 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435989573 news.xs4all.nl 2933 [2001:888:2000:d::a6]:41309 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93481 On Fri, Jul 3, 2015 at 9:14 AM, Marko Rauhamaa wrote: > >>> 1) is there a way to close just one direction of the connection? >> >> No. SOCK_STREAM sockets are always bidirectional. > > socket.shutdown(socket.SHUT_WR) does the trick. > > I think the asyncio.StreamWriter.write_eof() is the high-level > equivalent. I stand corrected. And you're also correct about write_eof: it causes shutdown(SHUT_WR) to be called on the underlying socket once the buffer has been written. https://hg.python.org/cpython/file/34460219c0e0/Lib/asyncio/selector_events.py#l737 That said, just replacing the writer.close() with writer.write_eof() in the OP's code doesn't seem to work; the server response comes back empty. This works: >>> sock1, sock2 = socket.socketpair() >>> sock1.send(b'REQUEST') 7 >>> sock1.shutdown(socket.SHUT_WR) >>> sock2.recv(100) b'REQUEST' >>> sock2.send(b'RESPONSE') 8 >>> sock1.recv(100) b'RESPONSE' And this works: import asyncio import socket def server(sock): request = yield from asyncio.get_event_loop().sock_recv(sock, 100) print("got request {!r}".format(request)) yield from asyncio.get_event_loop().sock_sendall(sock, b'RESPONSE') def client(sock): yield from asyncio.get_event_loop().sock_sendall(sock, b'REQUEST') sock.shutdown(socket.SHUT_WR) response = yield from asyncio.get_event_loop().sock_recv(sock, 100) print("got response {!r}".format(response)) asyncio.get_event_loop().stop() def connect(): clientsock, serversock = socket.socketpair() clientsock.setblocking(False) serversock.setblocking(False) asyncio.async(client(clientsock)) asyncio.async(server(serversock)) connect() asyncio.get_event_loop().run_forever() I'm wondering whether there might be a bug in the higher-level transport code that interferes with reading after calling write_eof.