Skip to main content

Extra Async

Utilities for Python Asynchronous programming

aenumerate

Asynchronous version of Python's "enumerate". Just pass an async-iterator where the iterator would be, and use it in an async-for.

usage:

import asyncio

from extraasync import aenumerate

async def paused_pulses(n, message="pulse", interval=0.1):
    for i in range(n):
        asyncio.sleep(interval)
        yield message

async def main():
    for index, message in aenumerate(paused_pulses(5)):
        print(index, message)

asyncio.run(main())

ExtraTaskGroup

An asyncio.TaskGroup subclass that won't cancel all tasks when any of them errors out.

The hardcoded behavior of asyncio.TaskGroup is that when any exception is raised in any of the taskgroup tasks, all sibling incomplete tasks get cancelled immediatelly.

With ExtraTaskGroup, all created tasks are run to completion, and any exceptions are bubbled up as ExceptionGroups on the host task.

import asyncio

from extraasync import ExtraTaskGroup

async def worker(n):
    await asyncio.sleep(n/10)
    if n % 3 == 0:
        raise RuntimeError()
    return n

async def main():
    try:
        async with ExtraTaskGroup() as tg:
            tasks = [tg.create_task(worker(i)) for i in range(10)]
    except *RuntimeError as exceptions:
        print(exceptions)

asyncio.run(main())

sync_to_async

Allows calling an async function from a synchronous context.

When called from a synchronous scenario, this will create and cache an asyncio.loop instance per thread for code that otherwise is not using asynchronous features. The plain use should permit synchronous code to use asynchronous networking libraries in a more efficient way than creating a new asyncio.loop at each call.

When called in an asynchronous scenario, where a synchronous function was previously called from an async context using the async_to_sync call, this will allow the nested call to happen in the context of that original loop. This enables the creation of a single code path to both async and asynchronous contexts. In other words, and async function which calls a synchronous function by awaiting the "async_to_sync" counterpart to this function can have the synchronous function call back asynchronous contexts using this call. This is the so called "bridge mode"

async def inner_async_function():
    """Runs in the same thread and loop as 'async_entry' """
    # await long async client API call
    await asyncio.sleep(0.01)
    return 23

def synchronous_shared_code_path(backend):
    if backend == "async":
        result = sync_to_async(inner_async_function)
    else:
        ...
        result = sync_call()

    return result

async def async_entry():
    # runs in a transparently created thread:
    result = await async_to_sync(synchronous_shared_code_path, kwargs={"backend": "async"}))

result = sync_to_async(blih)  # can be used in place of `asyncio.run`:
                        # for simple, non nested calls, the same
                        # asyncio loop is reused across calls.
assert result == 23

async_to_sync

Returns a future wrapping a synchronous call in other thread: the synchronous code will run concurrently while the asynchronous code keeps running, until the future returned is awaited.

This is similar to what asyncio.loop.run_in_executor do, but there are key differences:

Most important: synchronous code called this way CAN RESUME calling async co-routines in the same loop down the call chain by using the pair "sync_to_async" call, unlike ordinary "loop.run_in_executor".

Unlike "run_in_executor" this will create new threads as needed in an internal pool, so that no async task is stopped waiting for a free new thread in the pool. Subsequent, sequential, calls do get to reuse threads in this pool. This uncapped creation of threads is needed, because in case of a chained context switching between sync and async contexts in a call stack, not having an available thread would lead to a deadlock.

Also, the synchronous code is executed in a context (contextvars) copy from the current task - so that contextvars will work in the synchronous code. In a plain thread, as those used by the executor call, the sync code will see an empty context.

Returns a future that will contain the results of the synchronous call.

(check example snippet on sync_to_async description, above)

at_loop_stop_callback

Schedules a callback to when the asyncio loop is stopping

at_loop_stop_callback(callback, loop=None)

The callback is called without any parameters (use functools.partial if you need any) - and is called either at loop shutdown, or when the main task in execution with 'loop.run_until_complete' has finsihed.

More than one callback can be registered in this way - they will be called synchronously, in order.

returns a handle which can be used to unregister the callback with 'remove_loop_stop_callback'.

This project itself makes use of it to stop worker threads when an event loop is stopped.

It works by patching a private callback in asyncio.BaseEventLoop

remove_loop_stop_callback

Removes a scheduled callback for when the loop stops.

remove_loop_stop_callback(handle, loop=None)

This will simply cancel a previously registred callback for the current, or explicit loop.

Metadata

Release files for extraasync 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for extraasync 0.3.0
File Size Uploaded Provenance Actions
extraasync-0.3.0.tar.gz 14.3 kB With provenance Details for extraasync-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for extraasync 0.3.0
File Interpreter ABI Platform Actions
extraasync-0.3.0-py3-none-any.whl Python 3 none any Details for extraasync-0.3.0-py3-none-any.whl

Total release size: 26.2 kB

Release files / extraasync-0.3.0.tar.gz

Download URL extraasync-0.3.0.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cc55f2ebbb6d76e4df7c023057ba3a2a4f624c6102b2759cea5541f3d950b1bf
BLAKE2b-256 checksum
How to use checksums
801539e374dc7b8b817bba1a7053bc5ec2a33ff1ac152f9a278e704c075db6e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 3, 2025.

Transparency log

Release files / extraasync-0.3.0-py3-none-any.whl

Download URL extraasync-0.3.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bceb285009a94c9807835df339f6287a2af9ae23bbcc70e72df82b28631f1de6
BLAKE2b-256 checksum
How to use checksums
2748d817e880332ee772e68850e714982415df080b7c5c17269d5d36376f99ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 3, 2025.

Transparency log

Security for extraasync 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for extraasync-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 3, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page