Core
provide.foundation.concurrency.core
¶
TODO: Add module docstring.
Classes¶
Functions¶
async_gather
async
¶
Run awaitables concurrently with Foundation tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*awaitables
|
Awaitable[Any]
|
Awaitable objects to run concurrently |
()
|
return_exceptions
|
bool
|
If True, exceptions are returned as results |
False
|
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of results in the same order as input awaitables |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If no awaitables provided |
Example
import asyncio async def fetch_data(n): ... await async_sleep(0.1) ... return n * 2 async def main(): ... results = await async_gather( ... fetch_data(1), fetch_data(2), fetch_data(3) ... ) ... return results asyncio.run(main()) [2, 4, 6]
Source code in provide/foundation/concurrency/core.py
async_run
¶
Run async function with Foundation tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
main
|
Callable[[], Awaitable[Any]]
|
Async function to run |
required |
debug
|
bool
|
Whether to run in debug mode |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
Result of the main function |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If main is not callable |
Example
async def main(): ... await async_sleep(0.1) ... return "hello" result = async_run(main) result 'hello'
Source code in provide/foundation/concurrency/core.py
async_sleep
async
¶
Async sleep with Foundation tracking and cancellation support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delay
|
float
|
Number of seconds to sleep |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If delay is negative |
Example
import asyncio async def main(): ... await async_sleep(0.1) asyncio.run(main())
Source code in provide/foundation/concurrency/core.py
async_wait_for
async
¶
Wait for an awaitable with optional timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
awaitable
|
Awaitable[Any]
|
The awaitable to wait for |
required |
timeout
|
float | None
|
Timeout in seconds (None for no timeout) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Result of the awaitable |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If timeout is negative |
TimeoutError
|
If timeout is exceeded |
Example
import asyncio async def slow_task(): ... await async_sleep(0.2) ... return "done" async def main(): ... try: ... result = await async_wait_for(slow_task(), timeout=0.1) ... except asyncio.TimeoutError: ... result = "timed out" ... return result asyncio.run(main()) 'timed out'