API Reference

exception aiomisc.CircuitBroken(last_exception: Optional[Exception])[source]
class aiomisc.IteratorWrapper(gen_func: Callable[[], Generator[aiomisc.iterator_wrapper.T, aiomisc.iterator_wrapper.R, None]], loop: Optional[asyncio.events.AbstractEventLoop] = None, max_size: int = 0, executor: Optional[concurrent.futures._base.Executor] = None, statistic_name: Optional[str] = None)[source]
class aiomisc.IteratorWrapperSeparate(gen_func: Callable[[], Generator[aiomisc.iterator_wrapper.T, aiomisc.iterator_wrapper.R, None]], loop: Optional[asyncio.events.AbstractEventLoop] = None, max_size: int = 0, executor: Optional[concurrent.futures._base.Executor] = None, statistic_name: Optional[str] = None)[source]
class aiomisc.PeriodicCallback(coroutine_func: Callable[[...], Union[Awaitable[Any], Any]], *args: Any, **kwargs: Any)[source]

Note

When the periodic function executes longer then execution interval a next call will be skipping and warning will be logged.

class aiomisc.PoolBase(maxsize: int = 10, recycle: Optional[int] = None)[source]
class aiomisc.ProcessPoolExecutor(max_workers: int = 4, **kwargs: Any)[source]
shutdown(wait: bool = True) None[source]

Clean-up the resources associated with the Executor.

It is safe to call this method several times. Otherwise, no other methods can be called after this one.

Args:
wait: If True then shutdown will not return until all running

futures have finished executing and the resources used by the executor have been reclaimed.

submit(fn: Callable[[...], aiomisc.process_pool.T], *args: Any, **kwargs: Any) _asyncio.Future[source]

Submit blocking function to the pool

class aiomisc.ThreadPoolExecutor(max_workers: int = 4, loop: Optional[asyncio.events.AbstractEventLoop] = None, statistic_name: Optional[str] = None)[source]
shutdown(wait: bool = True) None[source]

Clean-up the resources associated with the Executor.

It is safe to call this method several times. Otherwise, no other methods can be called after this one.

Args:
wait: If True then shutdown will not return until all running

futures have finished executing and the resources used by the executor have been reclaimed.

submit(fn: aiomisc.thread_pool.F, *args: Any, **kwargs: Any) _asyncio.Future[source]

Submit blocking function to the pool

aiomisc.aggregate(leeway_ms: float, max_count: Optional[int] = None) Callable[source]

Parametric decorator that aggregates multiple (but no more than max_count defaulting to None) single-argument executions (res1 = await func(arg1), res2 = await func(arg2), …) of an asynchronous function with variadic positional arguments (async def func(*args, pho=1, bo=2) -> Iterable) into its single execution with multiple positional arguments (res1, res2, ... = await func(arg1, arg2, ...)) collected within a time window leeway_ms.

Note

func must return a sequence of values of length equal to the number of arguments (and in the same order).

Note

if some unexpected error occurs, exception is propagated to each future; to set an individual error for each aggregated call refer to aggregate_async.

Parameters
  • leeway_ms – The maximum approximate delay between the first collected argument and the aggregated execution.

  • max_count – The maximum number of arguments to call decorated function with. Default None.

Returns

aiomisc.aggregate_async(leeway_ms: float, max_count: Optional[int] = None) Callable[source]

Same as aggregate, but with func arguments of type Arg containing value and future attributes instead. In this setting func is responsible for setting individual results/exceptions for all of the futures or throwing an exception (it will propagate to futures automatically). If func mistakenly does not set a result of some future, then, ResultNotSetError exception is set.

Returns

aiomisc.asyncbackoff(attempt_timeout: Optional[Union[int, float]], deadline: Optional[Union[int, float]], pause: Union[int, float] = 0, *exc: Type[Exception], exceptions: Tuple[Type[Exception], ...] = (), max_tries: Optional[int] = None, giveup: Optional[Callable[[Exception], bool]] = None, statistic_name: Optional[str] = None, statistic_class: Type[aiomisc.backoff.BackoffStatistic] = <class 'aiomisc.backoff.BackoffStatistic'>) Callable[[Callable[[...], aiomisc.backoff.T]], Callable[[Callable[[...], aiomisc.backoff.T]], aiomisc.backoff.T]][source]

Patametric decorator that ensures that attempt_timeout and deadline time limits are met by decorated function.

In case of exception function will be called again with similar arguments after pause seconds.

Parameters
  • statistic_name – name filed for statistic instances

  • attempt_timeout – is maximum execution time for one execution attempt.

  • deadline – is maximum execution time for all execution attempts.

  • pause – is time gap between execution attempts.

  • exc – retrying when this exceptions was raised.

  • exceptions – similar as exc but keyword only.

  • max_tries – is maximum count of execution attempts (>= 1).

  • giveup – is a predicate function which can decide by a given

  • statistic_class – statistic class

aiomisc.asyncretry(max_tries: Optional[int], exceptions: Tuple[Type[Exception], ...] = (<class 'Exception'>,), pause: Union[int, float] = 0, giveup: Optional[Callable[[Exception], bool]] = None, statistic_name: Optional[str] = None) Callable[[Callable[[...], aiomisc.backoff.T]], Callable[[Callable[[...], aiomisc.backoff.T]], aiomisc.backoff.T]][source]

Shortcut of asyncbackoff(None, None, 0, Exception).

In case of exception function will be called again with similar arguments after pause seconds.

Parameters
  • max_tries – is maximum count of execution attempts (>= 1 or None means infinity).

  • exceptions – similar as exc but keyword only.

  • giveup – is a predicate function which can decide by a given

  • pause – is time gap between execution attempts.

  • statistic_name – name filed for statistic instances

aiomisc.awaitable(func: Callable[[...], Union[aiomisc.utils.T, Awaitable[aiomisc.utils.T]]]) Callable[[...], Awaitable[aiomisc.utils.T]][source]

Decorator wraps function and returns a function which returns awaitable object. In case than a function returns a future, the original future will be returned. In case then the function returns a coroutine, the original coroutine will be returned. In case than function returns non-awaitable object, it’s will be wrapped to a new coroutine which just returns this object. It’s useful when you don’t want to check function result before use it in await expression.

aiomisc.bind_socket(*args: Any, address: str, port: int = 0, options: Iterable[Tuple[int, int, int]] = (), reuse_addr: bool = True, reuse_port: bool = False, proto_name: Optional[str] = None) socket.socket[source]

Bind socket and set setblocking(False) for just created socket. This detects address format and select socket family automatically.

Parameters
  • args – which will be passed to stdlib’s socket constructor (optional)

  • address – bind address

  • port – bind port

  • options – Tuple of pairs which contain socket option to set and the option value.

  • reuse_addr – set socket.SO_REUSEADDR

  • reuse_port – set socket.SO_REUSEPORT

  • proto_name – protocol name which will be logged after binding

Returns

socket.socket

aiomisc.cancel_tasks(tasks: Iterable[_asyncio.Future]) _asyncio.Future[source]

All passed tasks will be cancelled and a new task will be returned.

Parameters

tasks – tasks which will be cancelled

aiomisc.chunk_list(iterable: Iterable[aiomisc.utils.T], size: int) Iterable[List[aiomisc.utils.T]][source]

Split list or generator by chunks with fixed maximum size.

async aiomisc.select(*awaitables: Awaitable[Any], return_exceptions: bool = False, cancel: bool = True, timeout: Optional[Union[int, float]] = None, wait: bool = True, loop: Optional[asyncio.events.AbstractEventLoop] = None) aiomisc.utils.SelectResult[source]
Parameters
  • awaitables – awaitable objects

  • return_exceptions – if True exception will not be raised just returned as result

  • cancel – cancel unfinished coroutines (default True)

  • timeout – execution timeout

  • wait – when False and cancel=True, unfinished coroutines will be cancelled in the background.

  • loop – event loop

aiomisc.shield(func: Callable[[...], Awaitable[aiomisc.utils.T]]) Callable[[...], Awaitable[aiomisc.utils.T]][source]

Simple and useful decorator for wrap the coroutine to asyncio.shield.

>>> @shield
... async def non_cancelable_func():
...     await asyncio.sleep(1)
class aiomisc_log.LogFormat(value)[source]

An enumeration.

class aiomisc_log.LogLevel(value)[source]

An enumeration.

class aiomisc_worker.PacketTypes(value)[source]

An enumeration.