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