duck.contrib.sync

Sync + Asynchronous helper tools.

This module provides utility functions to convert between synchronous and asynchronous functions using asgiref’s async_to_sync and custom Duck’s sync_to_async, with optional LRU caching to improve performance in high-frequency conversion scenarios.

Submodules

Package Contents

Functions

async_to_sync

Converts an asynchronous function into a synchronous one, with optional LRU caching.

convert_to_async_if_needed

Automatically converts a function to asynchronous if it’s synchronous, or returns it unchanged if it’s already a coroutine function.

convert_to_sync_if_needed

Automatically converts a coroutine function to synchronous if it’s asynchronous, or returns it unchanged if it’s already a non-coroutine function.

Data

__all__

ensure_async

ensure_sync

API

duck.contrib.sync.__all__

[‘iscoroutine’, ‘iscoroutinefunction’, ‘sync_to_async’, ‘async_to_sync’, ‘convert_to_async_if_needed…

duck.contrib.sync.async_to_sync(func: Callable) asgiref.sync.AsyncToSync[source]

Converts an asynchronous function into a synchronous one, with optional LRU caching.

Parameters:

func – The asynchronous function to convert.

Returns:

A synchronous version of the input async function.

Return type:

AsyncToSync

duck.contrib.sync.convert_to_async_if_needed(func: Callable) Callable[source]

Automatically converts a function to asynchronous if it’s synchronous, or returns it unchanged if it’s already a coroutine function.

Parameters:

func – The function to convert if needed.

Returns:

An async function if func was sync, otherwise the original.

Return type:

Callable

duck.contrib.sync.convert_to_sync_if_needed(func: Callable) Callable[source]

Automatically converts a coroutine function to synchronous if it’s asynchronous, or returns it unchanged if it’s already a non-coroutine function.

Parameters:

func – The function to convert if needed.

Returns:

A sync function if func was async, otherwise the original.

Return type:

Callable

duck.contrib.sync.ensure_async

None

duck.contrib.sync.ensure_sync

None