async-anywhere
async-anywhere is a set of tools which address common Python concurrency needs. It contains tools for bridging between any number of blocking threads and async event loops, tools for modelling state machines and state transitions, fork(2)-safety semantics for common concurrency primitives, and more.
If you are struggling with code that needs to coordinate between multiple threads and event loops, dealing with deadlocks caused by interacting with Future objects after a fork(2), or you need to maintain stateful/continuously-running async components of a larger program, this module may help!
It includes:
AwaitableFuture, aconcurrent.futures.Futurewhich can be blocked on orawaited on from any number of event loops or threads. It supports configurable cancellation behavior,fork(2)safety, lock-free operation on many paths that the stdlib serializes, and more.EventLoopExecutor, aconcurrent.futures.Executorwhich runsasynccode on a thread, and whose results can be waited on from anywhere. It supports idle expiration, calling into itself from within its captive thread,fork(2)safety, task cancellation, controlled shutdown, "parent" functions which own the event loop, and more.StateTracker, a "state machine lite" which allows a class to transition between one of several declared states, and allows other code to wait for the tracker to enter one or more states in anasync- and thread-agnostic way.- Several other tools and utilities related to Python concurrency management.
See "Examples" below, or the API documentation for more information.
Installation and Requirements
This module requires Python 3.13.3+, and can be installed from PyPI, with pip:
pip install async-anywhere
Or via uv:
uv add async-anywhere
async-anywhere is a pure-Python module with no required compiled components, and aims to have as few pure-python transitive dependencies as possible.
Examples
AwaitableFuture
from async_anywhere.futures import AwaitableFuture
from threading import Thread
import asyncio
loop = asyncio.new_event_loop()
fut = AwaitableFuture(
# Don't allow calls to .result() if this loop is running; that'll deadlock.
prevent_blocking_wait_within=[loop],
)
fut.add_done_callback(lambda f: print(f"Future {f} is done!"), run_in_forked_child=True)
async def foo():
print(await fut)
# Wait from one event loop:
Thread(target=asyncio.run, args=(foo(),)).start()
# ... two event loops:
Thread(target=asyncio.run, args=(foo(),)).start()
# Set the result from anywhere:
Thread(target=fut.set_result, args=("All done!",)).start()
# Wait from event loops without a wrapper:
print(loop.run_until_complete(fut))
# ... or synchronously:
print(fut.result(timeout=1))
EventLoopExecutor
from async_anywhere.executors import EventLoopExecutor
import asyncio
executor = EventLoopExecutor(idle_timeout=10) # Dies if not given work to do for 10 seconds
fut = executor.submit(asyncio.sleep, 1, result="Hello from a captive loop after 1 second!")
@executor.bridge
async def runs_in_captive_loop_thread():
print("Hello from a captive event loop on a dedicated thread!")
print(await executor.submit(asyncio.sleep, 1, result="Called from inside the executor!"))
print(await fut) # Await futures from anywhere
async def wait_for_executor():
await runs_in_captive_loop_thread() # Wait for results from async code
print(await fut) # Wait from futures from any loop/thread
asyncio.run(wait_for_executor())
print(fut.result(timeout=2)) # ...or from blocking code.
# Wait for executors to shut down from async code
async def finish():
await executor.shutdown(wait=False, cancel_futures=True)
# ...or synchronous code
executor.shutdown(wait=True, cancel_futures=True).result()
EventLoopExecutor is ideal for integrating third-party async code that wants to own the event loop/entry point (e.g. discordpy) into a larger program that may not be async, may block the event loop, or may use multiple event loop runtimes. Useful when you need more than asgiref.async_to_sync can provide.
Compatibility
This module is tested and used in production on Linux and MacOS systems. BSDs are likely supported, but tests on those systems have not yet been performed. Windows support is planned.
async-anywhere supports:
- Code written for
asyncio. - Code written for
trio, though Trio support is a bit fussy to use and will be improved in a future release. - Code written for
anyio. - Code using
AbstractEventLoop, includinguvloop. - Correct, minimally locking operations with or without the Global Interpreter Lock (GIL).
Code using nest-asyncio, or other tools which monkeypatch or mock out core asyncio primitives, may not work well with this library.
When should you use this module?
This module can be used in a variety of situations, including uses where async Python code isn't in use at all. Each of the tools in this module has affinity for particular use cases, and may not be appropriate for others.
EventLoopExecutoris the most powerful tool in this module, and is useful when you need to invokeasynccode from a combination of synchronous code, multiple threads, and multiple different event loops, andasgiref.async_to_syncisn't sufficient (when to useEventLoopExecutor).AwaitableFutureis useful if you have code which heavily usesasyncio.Futureorconcurrent.futures.Futuredirectly, and you want a friendlier (and more performant in many cases) API (when to useAwaitableFuture).StateTrackeris useful for code that wants to model "state machines without hooks": code that transitions between states in a graph, but doesn't want to model all of its code as hooks on particular state transitions (when to useStateTracker).
For how EventLoopExecutor compares to other tools that bridge synchronous and async code, see Prior Art.
Fork Safety
The different subcomponents of this module are fork-safe, in that they have documented semantics and avoid unexpected blocking or state corruption in the presence of code that uses os.fork():
Note that care is still needed when forking in the presence of this module or concurrent Python code generally; read
the above carefully in order to understand tradeoffs related to fork(2).
Development and Contributing
See CONTRIBUTING.md
LLM usage
Code in this module was written by hand. LLMs are sometimes used by maintainers to identify issues, prototype changes, or write tests.
However, all non-test generated code and documentation is reviewed and re-written by hand before being released (even if that means re-typing it verbatim). No exceptions. We believe that the mechanical act of code authorship itself benefits comprehension and maintainability, and thus will hand-write/transcribe all non-test code even when LLM outputs are correct.
Code supplied by contributors will be reviewed regardless of provenance, but we request that contributors participate in the contribution process as humans and not rely exclusively on agents, regardless of who wrote the code.
License
async-anywhere is free and open source software. See LICENSE for more information.
Metadata
Release files for async-anywhere 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| async_anywhere-0.0.1.tar.gz | 60.6 kB | With provenance | Details for async_anywhere-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| async_anywhere-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for async_anywhere-0.0.1-py3-none-any.whl |
Total release size: 125.9 kB
Release files / async_anywhere-0.0.1.tar.gz
| Download URL | async_anywhere-0.0.1.tar.gz |
|---|---|
| Size | 60.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
76123294a76d4a5a870890298b7310fdffb91ccb0f24136f07da2d1c37428fed
|
|
BLAKE2b-256 checksum How to use checksums |
95301ebb22cc8965bea15e8c7a77b08b593144e8df306413f750466700b5cb74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 23, 2026.
Transparency logRelease files / async_anywhere-0.0.1-py3-none-any.whl
| Download URL | async_anywhere-0.0.1-py3-none-any.whl |
|---|---|
| Size | 65.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
77a6f2e0bd117e957959516b10d470996adac989469d55d63af9f785f86e84a8
|
|
BLAKE2b-256 checksum How to use checksums |
1d0b879604b7bdbc68e20b6bc129fa797acba2f456851d51ea23fac3c492463f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 23, 2026.
Transparency logSecurity for async-anywhere 0.0.1
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.
Attestation for async_anywhere-0.0.1.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 Nov 23, 2026.
Transparency logA 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.