asyncio await different coroutines on the same socket?
Russell Owen
rowen at uw.edu
Wed Oct 3 09:44:35 EDT 2018
Using asyncio I am looking for a simple way to await multiple events where
notification comes over the same socket (or other serial stream) in arbitrary
order. For example, suppose I am communicating with a remote device that can
run different commands simultaneously and I don't know which command will
finish first. I want to do this:
coro1 = start(command1)
coro2 = start(command2)
asyncio.gather(coro1, coro2)
where either command may finish first. I’m hoping for a simple and
idiomatic way to read the socket and tell each coroutine it is done. So far
everything I have come up with is ugly, using multiple layers of "async
def”, keeping a record of Tasks that are waiting and calling "set_result"
on those Tasks when finished. Also Task isn’t even documented to have the
set_result method (though "future" is)
Is there a simple, idiomatic way to do this?
-- Russell
More information about the Python-list
mailing list