Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gather helper functions #148

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

tzoiker
Copy link
Contributor

@tzoiker tzoiker commented Sep 22, 2022

Adding several helper functions:

async def gather(
    *tocs: Optional[ToC],
    loop: Optional[AbstractEventLoop] = None,
    return_exceptions: bool = False,
)

Same as asyncio.gather but it is possible to safely pass Nones through.

gather_shackled(
    *tocs: Optional[ToC],
    wait_cancelled: bool = False,
) -> list

If any of the tasks fails – others are cancelled. Allows to wait for the completion of the cancelled tasks. Returns a list of values or raises the first exception.

async def gather_independent(
    *tocs: Optional[ToC],
    wait_cancelled: bool = False,
) -> list

If any of the tasks fails – it is ok. Allows to wait for the completion of the cancelled tasks in the case of external cancellation. Returns a list of values or exceptions.

async def gather_graceful(
    primary: Optional[Sequence[Optional[ToC]]] = None, *,
    secondary: Sequence[Optional[ToC]] = None,
    wait_cancelled: bool = False,
) -> Union[list, Tuple[list, list]]

Gather tasks in two groups - primary and secondary. If any primary is somehow failed, then, other tasks are cancelled. If secondary is failed – it is ok. If any primary is failed, then, will raise the first exception. Returns two lists of results, one for the primary tasks (only values) and the other for the secondary tasks (values or exceptions). If only primary or secondary is passed, then, returns a single list.

async def wait_graceful(
    primary: Optional[Iterable[Task]] = None,
    secondary: Optional[Iterable[Task]] = None,
    *,
    wait_cancelled: bool = False,
)

Main logic is the same as for gather_graceful.

async def wait_first_cancelled_or_exception(
    fs: Iterable[Future], *,
    loop: Optional[AbstractEventLoop] = None,
    timeout: float = None,
)

Similar to asyncio.wait, but particularly waits for the first cancelled or failed task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants