File tree 3 files changed +12
-6
lines changed 3 files changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -222,20 +222,26 @@ class StreamWriter:
222
222
def __init__ (self , s ):
223
223
self .s = s
224
224
225
- def write (self , buf ):
225
+ def awrite (self , buf ):
226
+ # This method is called awrite (async write) to not proliferate
227
+ # incompatibility with original asyncio. Unlike original asyncio
228
+ # whose .write() method is both not a coroutine and guaranteed
229
+ # to return immediately (which means it has to buffer all the
230
+ # data), this method is a coroutine.
226
231
sz = len (buf )
227
232
while True :
228
233
res = self .s .write (buf )
229
- log .debug ("StreamWriter.write(): %d" , res )
230
- # If we spooled everything, (just) return
234
+ # If we spooled everything, return immediately
231
235
if res == sz :
236
+ log .debug ("StreamWriter.awrite(): completed spooling %d bytes" , res )
232
237
return
233
238
if res is None :
234
239
res = 0
240
+ log .debug ("StreamWriter.awrite(): spooled partial %d bytes" , res )
235
241
buf = buf [res :]
236
242
sz -= res
237
243
s = yield IOWrite (self .s )
238
- log .debug ("StreamWriter.write (): can write more" )
244
+ log .debug ("StreamWriter.awrite (): can write more" )
239
245
240
246
def close (self ):
241
247
yield IODone (IO_WRITE , self .s )
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ def print_http_headers(url):
6
6
print (reader , writer )
7
7
print ("================" )
8
8
query = "GET / HTTP/1.0\r \n \r \n "
9
- yield from writer .write (query .encode ('latin-1' ))
9
+ yield from writer .awrite (query .encode ('latin-1' ))
10
10
while True :
11
11
line = yield from reader .readline ()
12
12
if not line :
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ def serve(reader, writer):
5
5
print (reader , writer )
6
6
print ("================" )
7
7
print ((yield from reader .read ()))
8
- yield from writer .write ("HTTP/1.0 200 OK\r \n \r \n Hello.\r \n " )
8
+ yield from writer .awrite ("HTTP/1.0 200 OK\r \n \r \n Hello.\r \n " )
9
9
print ("After response write" )
10
10
yield from writer .close ()
11
11
print ("Finished processing request" )
You can’t perform that action at this time.
0 commit comments