Concurrency Utilities and Patterns

async for ... in usim.first(*activities: Coroutine[Any, Any, RT], count: Optional[int] = 1) → AsyncIterator[RT][source]

Run all activities concurrently to get the first count results available

Parameters
  • activities – activities to run concurrently

  • count – maximum number of results

Returns

async iterable of results

Raises
  • usim.Concurrent – if any of the activities raise an exception

  • ValueError – if count is bigger than number of activities

If there are more results than count, any remaining activities are aborted after yielding the last result. If there are less results than count, the iterator finishes after yielding the last result. If count is None, the iterator provides all results.

Results are always yielded in the order of becoming available. The initial order of activities is irrelevant.

await usim.collect(*activities: Coroutine[Any, Any, RT]) → List[RT][source]

Run all activities concurrently to provide all results

Parameters

activities – activities to run concurrently

Returns

list of results

Raises

usim.Concurrent – if any of the activities raise an exception

Results are always yielded in the order of the activities producing them; the order at which individual activities finish is irrelevant. However, results are only available after all activities are finished.