From d0d2810b0cf940ee56e246a9ce9ffffc866229f0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 1 Jun 2014 00:41:45 +0300 Subject: [PATCH] asyncio_micro: Rename StreamWriter.write() to awrite(). This method has different semantics than original asyncio, so rename to avoid confusion. Original asyncio's is not a coroutine, while ours is. --- asyncio_micro/asyncio_micro.py | 14 ++++++++++---- asyncio_micro/test_http_client.py | 2 +- asyncio_micro/test_http_server.py | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/asyncio_micro/asyncio_micro.py b/asyncio_micro/asyncio_micro.py index f7ddb921..a46b3d25 100644 --- a/asyncio_micro/asyncio_micro.py +++ b/asyncio_micro/asyncio_micro.py @@ -222,20 +222,26 @@ class StreamWriter: def __init__(self, s): self.s = s - def write(self, buf): + def awrite(self, buf): + # This method is called awrite (async write) to not proliferate + # incompatibility with original asyncio. Unlike original asyncio + # whose .write() method is both not a coroutine and guaranteed + # to return immediately (which means it has to buffer all the + # data), this method is a coroutine. sz = len(buf) while True: res = self.s.write(buf) - log.debug("StreamWriter.write(): %d", res) - # If we spooled everything, (just) return + # If we spooled everything, return immediately if res == sz: + log.debug("StreamWriter.awrite(): completed spooling %d bytes", res) return if res is None: res = 0 + log.debug("StreamWriter.awrite(): spooled partial %d bytes", res) buf = buf[res:] sz -= res s = yield IOWrite(self.s) - log.debug("StreamWriter.write(): can write more") + log.debug("StreamWriter.awrite(): can write more") def close(self): yield IODone(IO_WRITE, self.s) diff --git a/asyncio_micro/test_http_client.py b/asyncio_micro/test_http_client.py index eb7a34c0..e83cf859 100644 --- a/asyncio_micro/test_http_client.py +++ b/asyncio_micro/test_http_client.py @@ -6,7 +6,7 @@ def print_http_headers(url): print(reader, writer) print("================") query = "GET / HTTP/1.0\r\n\r\n" - yield from writer.write(query.encode('latin-1')) + yield from writer.awrite(query.encode('latin-1')) while True: line = yield from reader.readline() if not line: diff --git a/asyncio_micro/test_http_server.py b/asyncio_micro/test_http_server.py index 5b8271d6..c2210767 100644 --- a/asyncio_micro/test_http_server.py +++ b/asyncio_micro/test_http_server.py @@ -5,7 +5,7 @@ def serve(reader, writer): print(reader, writer) print("================") print((yield from reader.read())) - yield from writer.write("HTTP/1.0 200 OK\r\n\r\nHello.\r\n") + yield from writer.awrite("HTTP/1.0 200 OK\r\n\r\nHello.\r\n") print("After response write") yield from writer.close() print("Finished processing request")