Hey everyone. I have been looking into asyncio lately, and even though I
have had my fair share of work, I still have some of it very shaky, so
first of all forgive me if what I am saying here is already implemented and
I totally missed it (so far, it looks *really* likely).

Basically this is the situation: I have an application that listens on two
websockets through the async library https://websockets.readthedocs.io/ and
I have to perform the same function on the result, no matter where the
message came from. I understand that I can create two coroutines that call
the same function, but it would be much cleaner (because of implementation
issues) if I can simply create a coroutine that yields the result of
whichever connection arrives first.

I have implemented a rather cumbersome solution with async Queues (as I
would do in threading), in which each connection puts its message in a
queue and an adapter class awaits the first element of the queue on
"receive". Here I attach a pastebin with the minimal working example:
https://pastebin.com/BzaxRbtF

However, it looks like a more async-friendly solution should exist,
something like

```
async def _start():
    msg1 = recv("Messager 1", sleep_time=1)
    msg2 = recv("Messager 2", sleep_time=2)
    while True:
        result = await asyncio.on_first_return(msg1, msg2)
        print(result)
```

(I understand that this implementation would not work because the event
loop doesn't know that it is "the same task repeated", but it's just to
tell you the general idea)

Again, it's quite likely I am not seeing something obvious, but I didn't
know where else to ask.

Thank you very much,
Pablo
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XBR5QPXRBCCJELDVEWMKRBPTNG4SJM64/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to