Hi All,
I'm trying to figure out The Right Way to wait on an asynchronous result
from two different asyncio-enabled libraries. At first glance (and my
first implementation of a solution to my problem), it seems like I want to
use something like the following:
# given a server that shuttles messages between a websocket and a redis
server
# wait until the next message arrives, from whichever source, and send
it to the other
ws_coro = websocket.recv()
redis_coro = redis_subscriber.next_published()
done, pending = yield from asyncio.wait([ws_coro, redis_coro],
return_when=asyncio.FIRST_COMPLETED)
for task in done:
if task._coro == ws_coro:
yield from send_to_redis(task.result())
elif task._coro == redis_coro:
yield from send_to_websocket(task.result())
Indeed, this works, except that the behavior that comes after
asyncio.wait() seems a little suspect to me. To differentiate between the
returned result, I have to use the private attribute `task._coro`. Is that
really the intended use of asyncio.wait? Is there something more
appropriate to my situation?
To recap, I want to wait on the first completed task of two (or more)
heterogenous task types. And then I want to process results from each task
type in a distinct way.
I'd gladly welcome any advice on the topic.
--
Don