Martin,

I am still wondering, what is your actual use case? Multiple coroutines
writing to the same stream sounds like the exception, not the common case
-- even when you pay careful attention to the appearance of yield-from, you
still have the problem that you can't control which of several coroutines
runs first. I must assume that you have a protocol over TCP/IP that has
some kind of framing convention, and you always ensure that a coroutine
writes an entire frame without yielding.

I wonder if you could just a bare Transport/Protocol pair instead of a
StreamWriter? In that case you'd have to implement pause_writing() and
resume_writing() in your Protocol class if you want to have the equivalent
of drain(). (But do you? Is there nothing at a higher level in your
protocol to prevent your writing coroutines from overwhelming the other end
of the socket?)

In any case, a simple solution for ensuring only one coroutine calls
drain() at a time would be to associate an asyncio.Lock with the stream --
then you can do

  with (yield from lock):
      yield from writer.drain()

-- 
--Guido van Rossum (python.org/~guido)

Reply via email to