On Monday, August 25, 2014 2:03:36 PM UTC+2, Victor Stinner wrote:
>
> Hi, 
>
> It's probably a bug. 
>

Ok, should I open an issue? 
 

> > I also have questions about StreamWriter and the flow control system. 
> > 
> > I understood that I am expected to yield from writer.drain() after any 
> call 
> > to writer.write(), so the flow control mechanism can make the calling 
> task 
> > wait until the buffer gets downsized to the low-water limit. 
>
> Nope, see the documentation: 
>
> "drain(): 
> Wait until the write buffer of the underlying transport is flushed." 
>  
>
> I don't 
> > understand why the writer.write[lines]() functions are not coroutines 
> which 
> > actually yield from writer.drain(), nor why the "yield from 
> writer.drain()" 
> > is not performed before the call to write(). 
>
> The purpose of a buffer is performances. You may be able to pack 
> multiple small writes into a single call to socket.send(). Flushing 
> after each call to stream.write() would call socket.send() each time, 
> which is less efficient. 
>
 
That is what the documentation says, but it's almost a contradiction with 
the next sentence: I fail to understand why it doesn't wait if the protocol 
is not paused. The protocol will only be paused if the buffer reaches the 
high water limit, thus, drain() will indeed not wait for the underlying 
buffer to be flushed in most cases.
If this is true, I also don't get how we can be notified that the 
high-water limit has been reached using StreamWriter(). If as a user i keep 
calling write(), I can always fill my buffer without knowing that the other 
end can not keep up.
 

> In fact, you don't need to wait for drain(), asyncio automatically 
> flushs the buffer "in background". drain() is only required when you 
> have to respect a protocol, for example write and then read when the 
> write is done. 
>  

> On a related topic, is there a reason why StreamWriter does not have a 
> > flush() coroutine, or any other way to wait until the buffer is empty? 
> The 
> > only workaround I've got for this is to temporarily force high and low 
> water 
> > limits to 0 so writer.drain() will wait until the buffer is actually 
> empty. 
>
> Limits are only used to pause the protocol. The protocol is not 
> directly related to the buffer. 
>

So on which object do these limits apply?

An example of situation which I can't solve is when I want to run the loop 
until the transport wrote everything. I think there is currently no way to 
synchronize on this event.

Reply via email to