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.
pull/11/head
Paul Sokolovsky 2014-06-01 00:41:45 +03:00
rodzic 9c3a3cd5ed
commit d0d2810b0c
3 zmienionych plików z 12 dodań i 6 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -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:

Wyświetl plik

@ -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")