question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`gather` and `select` to replace `Combine` and `First`

See original GitHub issue

Combine and First only work on triggers and tasks, not on coroutines or any other kind of awaitable. This is kind of limiting. You can work around it by wrapping coroutine arguments in fork and wrapping awaitables in coroutine functions and fork 🤢. They also have crappy semantics. Combine returns nothing. First returns only the result of the first await to complete. There is no way of knowing which input fired. First also does not handle cancelling tasks that are passed in, which is very annoying when you are simply using tasks to work-around the fact that First doesn’t take coroutines or awaitables.

I propose two new interfaces to replace Combine and First respectively: gather and select. gather will run any given awaitables concurrently and return a list of the results of each await statement. select will run any given awaitables concurrently, and return once the first of the awaitables finishes. It will return the index (by position of the arguments) of the awaitable that fired, and the result of the await statement. Like Combine and First, exceptions will be propagated by default, and the awaitables that did not throw an exception will be cancelled safely.

async def gather(*args: Awaitable[Any]) -> List[Any]:
    ...

async def select(*args: Awaitable[Any]) -> Tuple[int, Any]:
    ...

gather is extremely similar to asyncio.gather. There is no interface in asyncio for something like gather, but only for the first awaitable to finish. I decided on the word select because it is similar to the select statement in Go, albeit not just for queues.

Once these interface are implemented and tested, perhaps Combine and First should be deprecated. This is simply so that there is “one way to do it”.

A follow-on may add an interface for catching exceptions as results rather than propagating them, much like asyncio.gather supports.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
marlonjamescommented, Aug 11, 2021

cocotb has accumulated a lot of deprecations over the past few versions. We try not to break things on minor versions, so 2.0 should get rid of those, plus some other breaking changes that are sorely needed.

0reactions
ktbarrettcommented, Sep 4, 2021

There are issues with asyncio.wait. As soon as you segregate the futures into two different lists you obliterate any information matching the inputs to the outputs. Such a function should return a single list so that users can map outputs back to inputs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gather multiple sets of columns - Stack Overflow
First gather all question columns, use extract() to separate into question and loop_number , then spread() question back into the columns.
Read more >
Gather columns into key-value pairs - tidyr - Tidyverse
A selection of columns. If empty, all variables are selected. You can supply bare variable names, select all variables between x and z...
Read more >
Data Wrangling in R: Combining, Merging and Reshaping Data
Let's say we want to merge the first and left data frames by x0 and id. The # by.x and by.y arguments specify...
Read more >
Tutorial: Shape and combine data in Power BI Desktop
In this tutorial, you learn how to shape and combine data in Power BI ... To change them, right-click the column header, and...
Read more >
Group by: split-apply-combine — pandas 1.5.2 documentation
SELECT Column1, Column2, mean(Column3), sum(Column4) FROM SomeTable GROUP BY ... In [49]: s.groupby(["first", "second"]).sum() Out[49]: first second bar doo ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found