git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

asyncio await different coroutines on the same socket?


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