duck.utils.threadingยถ

Threading utilities and helpers.

Submodulesยถ

Package Contentsยถ

Classesยถ

SyncFuture

A thread-safe future that blocks until a result is set or an exception is raised.

Functionsยถ

async_to_sync_future

Converts an asynchronous future to a synchronous future.

get_max_workers

Dynamically calculate a safe max_workers value for ThreadPoolExecutor, based on CPU count, available memory, stack size, and current system usage. Works cross-platform (Linux, Windows, macOS). No root required.

APIยถ

class duck.utils.threading.SyncFuture[source]ยถ

A thread-safe future that blocks until a result is set or an exception is raised.

This class mimics a subset of the behavior of asyncio.Future, but for use in synchronous (threaded) code. It allows one thread to wait for a value or error that will be provided by another thread.

Initialization

Initializes the SyncFuture with no result or exception.

exception() โ†’ Optional[Exception][source]ยถ

Returns the future exception if set.

result()[source]ยถ

Blocks until a result or exception is set, then returns or raises it.

Returns:

The result value previously set by set_result().

Return type:

Any

Raises:

Exception โ€“ If an exception was set using set_exception().

set_exception(exception)[source]ยถ

Sets an exception for the future and unblocks any waiting thread.

Parameters:

exception โ€“ The exception to raise when result() is called.

set_result(value)[source]ยถ

Sets the result of the future and unblocks any waiting thread.

Parameters:

value โ€“ The result to return from the result() method.

duck.utils.threading.async_to_sync_future(async_future: asyncio.Future) โ†’ SyncFuture[source]ยถ

Converts an asynchronous future to a synchronous future.

duck.utils.threading.get_max_workers() โ†’ int[source]ยถ

Dynamically calculate a safe max_workers value for ThreadPoolExecutor, based on CPU count, available memory, stack size, and current system usage. Works cross-platform (Linux, Windows, macOS). No root required.

Returns:

Suggested max_workers value (min 8, max 2000)

Return type:

int