async-anywhere 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- async_anywhere-0.0.1/LICENSE +21 -0
- async_anywhere-0.0.1/PKG-INFO +188 -0
- async_anywhere-0.0.1/README.md +159 -0
- async_anywhere-0.0.1/pyproject.toml +168 -0
- async_anywhere-0.0.1/src/async_anywhere/__init__.py +7 -0
- async_anywhere-0.0.1/src/async_anywhere/_util/__init__.py +214 -0
- async_anywhere-0.0.1/src/async_anywhere/_util/fork_safety.py +23 -0
- async_anywhere-0.0.1/src/async_anywhere/_util/trio.py +31 -0
- async_anywhere-0.0.1/src/async_anywhere/exceptions.py +195 -0
- async_anywhere-0.0.1/src/async_anywhere/executors/__init__.py +862 -0
- async_anywhere-0.0.1/src/async_anywhere/executors/_fork_safety.py +15 -0
- async_anywhere-0.0.1/src/async_anywhere/executors/_job.py +65 -0
- async_anywhere-0.0.1/src/async_anywhere/executors/_timer.py +21 -0
- async_anywhere-0.0.1/src/async_anywhere/executors/singleton.py +258 -0
- async_anywhere-0.0.1/src/async_anywhere/futures/__init__.py +805 -0
- async_anywhere-0.0.1/src/async_anywhere/futures/_asyncio.py +80 -0
- async_anywhere-0.0.1/src/async_anywhere/futures/_chain.py +132 -0
- async_anywhere-0.0.1/src/async_anywhere/futures/_fork_safety.py +27 -0
- async_anywhere-0.0.1/src/async_anywhere/futures/_trio.py +39 -0
- async_anywhere-0.0.1/src/async_anywhere/misc.py +57 -0
- async_anywhere-0.0.1/src/async_anywhere/state_tracker/__init__.py +524 -0
- async_anywhere-0.0.1/src/async_anywhere/state_tracker/waitable.py +158 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zac Bentley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: async_anywhere
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Interact with your async code from any thread or event loop
|
|
5
|
+
Author-email: Zac Bentley <zbentley@users.noreply.github.com>
|
|
6
|
+
Requires-Python: >=3.13.3
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Framework :: AsyncIO
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: MacOS
|
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: attrs>=23.2.0
|
|
20
|
+
Requires-Dist: sniffio>=1.3.0,<2.0
|
|
21
|
+
Project-URL: Changelog, https://github.com/zbentley/async-anywhere/releases
|
|
22
|
+
Project-URL: Documentation, https://async-anywhere.readthedocs.io
|
|
23
|
+
Project-URL: Homepage, https://github.com/zbentley/async-anywhere
|
|
24
|
+
Project-URL: Issues, https://github.com/zbentley/async-anywhere/issues
|
|
25
|
+
Project-URL: Repository, https://github.com/zbentley/async-anywhere
|
|
26
|
+
Project-URL: Source, https://github.com/zbentley/async-anywhere
|
|
27
|
+
Import-Name: async_anywhere
|
|
28
|
+
|
|
29
|
+
# async-anywhere
|
|
30
|
+
|
|
31
|
+
[](https://github.com/zbentley/async-anywhere/actions/workflows/test.yml?query=branch%3Amain)
|
|
32
|
+
[](https://github.com/zbentley/async-anywhere/actions/workflows/black.yml?query=branch%3Amain)
|
|
33
|
+
[](https://pypi.org/project/async-anywhere/)
|
|
34
|
+
[](https://async-anywhere.readthedocs.io/en/latest/)
|
|
35
|
+

|
|
36
|
+
[](https://github.com/zbentley/async-anywhere/blob/main/.github/workflows/test.yml)
|
|
37
|
+

|
|
38
|
+
|
|
39
|
+
`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.
|
|
40
|
+
|
|
41
|
+
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!
|
|
42
|
+
|
|
43
|
+
It includes:
|
|
44
|
+
|
|
45
|
+
- [`AwaitableFuture`](https://async-anywhere.readthedocs.io/en/latest/reference/futures/awaitable.html#async_anywhere.futures.AwaitableFuture), a `concurrent.futures.Future` which can be blocked on or `await`ed 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.
|
|
46
|
+
- [`EventLoopExecutor`](https://async-anywhere.readthedocs.io/en/latest/reference/executors/EventLoopExecutor.html#async_anywhere.executors.EventLoopExecutor), a `concurrent.futures.Executor` which runs **`async`** code 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.
|
|
47
|
+
- [`StateTracker`](https://async-anywhere.readthedocs.io/en/latest/reference/state_tracker/StateTracker.html#async_anywhere.state_tracker.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 an `async`- and thread-agnostic way.
|
|
48
|
+
- Several other tools and utilities related to Python concurrency management.
|
|
49
|
+
|
|
50
|
+
See "Examples" below, or [the API documentation](https://async-anywhere.readthedocs.io/en/latest/reference/index.html) for more information.
|
|
51
|
+
|
|
52
|
+
## Installation and Requirements
|
|
53
|
+
|
|
54
|
+
This module requires Python 3.13.3+, and can be installed from [PyPI](https://pypi.org/project/async-anywhere/), with `pip`:
|
|
55
|
+
|
|
56
|
+
```console
|
|
57
|
+
pip install async-anywhere
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Or via `uv`:
|
|
61
|
+
|
|
62
|
+
```console
|
|
63
|
+
uv add async-anywhere
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`async-anywhere` is a pure-Python module with no required compiled components, and aims to have as few pure-python transitive dependencies as possible.
|
|
67
|
+
|
|
68
|
+
## Examples
|
|
69
|
+
|
|
70
|
+
### `AwaitableFuture`
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from async_anywhere.futures import AwaitableFuture
|
|
74
|
+
from threading import Thread
|
|
75
|
+
import asyncio
|
|
76
|
+
|
|
77
|
+
loop = asyncio.new_event_loop()
|
|
78
|
+
fut = AwaitableFuture(
|
|
79
|
+
# Don't allow calls to .result() if this loop is running; that'll deadlock.
|
|
80
|
+
prevent_blocking_wait_within=[loop],
|
|
81
|
+
)
|
|
82
|
+
fut.add_done_callback(lambda f: print(f"Future {f} is done!"), run_in_forked_child=True)
|
|
83
|
+
|
|
84
|
+
async def foo():
|
|
85
|
+
print(await fut)
|
|
86
|
+
|
|
87
|
+
# Wait from one event loop:
|
|
88
|
+
Thread(target=asyncio.run, args=(foo(),)).start()
|
|
89
|
+
# ... two event loops:
|
|
90
|
+
Thread(target=asyncio.run, args=(foo(),)).start()
|
|
91
|
+
# Set the result from anywhere:
|
|
92
|
+
Thread(target=fut.set_result, args=("All done!",)).start()
|
|
93
|
+
# Wait from event loops without a wrapper:
|
|
94
|
+
print(loop.run_until_complete(fut))
|
|
95
|
+
# ... or synchronously:
|
|
96
|
+
print(fut.result(timeout=1))
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### `EventLoopExecutor`
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from async_anywhere.executors import EventLoopExecutor
|
|
103
|
+
import asyncio
|
|
104
|
+
|
|
105
|
+
executor = EventLoopExecutor(idle_timeout=10) # Dies if not given work to do for 10 seconds
|
|
106
|
+
|
|
107
|
+
fut = executor.submit(asyncio.sleep, 1, result="Hello from a captive loop after 1 second!")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@executor.bridge
|
|
111
|
+
async def runs_in_captive_loop_thread():
|
|
112
|
+
print("Hello from a captive event loop on a dedicated thread!")
|
|
113
|
+
print(await executor.submit(asyncio.sleep, 1, result="Called from inside the executor!"))
|
|
114
|
+
print(await fut) # Await futures from anywhere
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async def wait_for_executor():
|
|
118
|
+
await runs_in_captive_loop_thread() # Wait for results from async code
|
|
119
|
+
print(await fut) # Wait from futures from any loop/thread
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
asyncio.run(wait_for_executor())
|
|
123
|
+
print(fut.result(timeout=2)) # ...or from blocking code.
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
# Wait for executors to shut down from async code
|
|
127
|
+
async def finish():
|
|
128
|
+
await executor.shutdown(wait=False, cancel_futures=True)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# ...or synchronous code
|
|
132
|
+
executor.shutdown(wait=True, cancel_futures=True).result()
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`EventLoopExecutor` is ideal for integrating third-party `async` code that wants to own the event loop/entry point (e.g. [`discordpy`](https://discordpy.readthedocs.io/en/stable/quickstart.html)) 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.
|
|
137
|
+
|
|
138
|
+
### Compatibility
|
|
139
|
+
|
|
140
|
+
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.
|
|
141
|
+
|
|
142
|
+
`async-anywhere` supports:
|
|
143
|
+
|
|
144
|
+
- Code written for `asyncio`.
|
|
145
|
+
- Code written for `trio`, though Trio support is a bit fussy to use and will be improved in a future release.
|
|
146
|
+
- Code written for `anyio`.
|
|
147
|
+
- Code using `AbstractEventLoop`, including `uvloop`.
|
|
148
|
+
- Correct, minimally locking operations with or without the Global Interpreter Lock (GIL).
|
|
149
|
+
|
|
150
|
+
Code using [nest-asyncio](https://pypi.org/project/nest-asyncio/), or other tools which monkeypatch or mock out core `asyncio` primitives, may not work well with this library.
|
|
151
|
+
|
|
152
|
+
## When should you use this module?
|
|
153
|
+
|
|
154
|
+
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.
|
|
155
|
+
|
|
156
|
+
- `EventLoopExecutor` is the most powerful tool in this module, and is useful when you need to invoke `async` code from a combination of synchronous code, multiple threads, and multiple different event loops, and `asgiref.async_to_sync` isn't sufficient ([when to use `EventLoopExecutor`](https://async-anywhere.readthedocs.io/en/latest/reference/executors/when_to_use.html)).
|
|
157
|
+
- `AwaitableFuture` is useful if you have code which heavily uses `asyncio.Future` or `concurrent.futures.Future` directly, and you want a friendlier (and more performant in many cases) API ([when to use `AwaitableFuture`](https://async-anywhere.readthedocs.io/en/latest/reference/futures/when_to_use.html)).
|
|
158
|
+
- `StateTracker` is 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 use `StateTracker`](https://async-anywhere.readthedocs.io/en/latest/reference/state_tracker/when_to_use.html)).
|
|
159
|
+
|
|
160
|
+
For how `EventLoopExecutor` compares to other tools that bridge synchronous and `async` code, see [Prior Art](https://async-anywhere.readthedocs.io/en/latest/reference/executors/prior_art.html).
|
|
161
|
+
|
|
162
|
+
## Fork Safety
|
|
163
|
+
|
|
164
|
+
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()`:
|
|
165
|
+
|
|
166
|
+
- [`EventLoopExecutor` fork safety](https://async-anywhere.readthedocs.io/en/latest/reference/executors/fork_safety.html).
|
|
167
|
+
- [`AwaitableFuture` fork safety](https://async-anywhere.readthedocs.io/en/latest/reference/futures/fork_safety.html).
|
|
168
|
+
- [`StateTracker` fork safety](https://async-anywhere.readthedocs.io/en/latest/reference/state_tracker/fork_safety.html).
|
|
169
|
+
|
|
170
|
+
**Note** that care is still needed when forking in the presence of this module or concurrent Python code generally; read
|
|
171
|
+
the above carefully in order to understand tradeoffs related to `fork(2)`.
|
|
172
|
+
|
|
173
|
+
## Development and Contributing
|
|
174
|
+
|
|
175
|
+
See [CONTRIBUTING.md](https://github.com/zbentley/async-anywhere/blob/main/CONTRIBUTING.md)
|
|
176
|
+
|
|
177
|
+
### LLM usage
|
|
178
|
+
|
|
179
|
+
Code in this module was written by hand. LLMs are sometimes used by maintainers to identify issues, prototype changes, or write tests.
|
|
180
|
+
|
|
181
|
+
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.
|
|
182
|
+
|
|
183
|
+
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.
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
`async-anywhere` is free and open source software. See [`LICENSE`](https://github.com/zbentley/async-anywhere/blob/main/LICENSE) for more information.
|
|
188
|
+
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# async-anywhere
|
|
2
|
+
|
|
3
|
+
[](https://github.com/zbentley/async-anywhere/actions/workflows/test.yml?query=branch%3Amain)
|
|
4
|
+
[](https://github.com/zbentley/async-anywhere/actions/workflows/black.yml?query=branch%3Amain)
|
|
5
|
+
[](https://pypi.org/project/async-anywhere/)
|
|
6
|
+
[](https://async-anywhere.readthedocs.io/en/latest/)
|
|
7
|
+

|
|
8
|
+
[](https://github.com/zbentley/async-anywhere/blob/main/.github/workflows/test.yml)
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
`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.
|
|
12
|
+
|
|
13
|
+
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!
|
|
14
|
+
|
|
15
|
+
It includes:
|
|
16
|
+
|
|
17
|
+
- [`AwaitableFuture`](https://async-anywhere.readthedocs.io/en/latest/reference/futures/awaitable.html#async_anywhere.futures.AwaitableFuture), a `concurrent.futures.Future` which can be blocked on or `await`ed 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.
|
|
18
|
+
- [`EventLoopExecutor`](https://async-anywhere.readthedocs.io/en/latest/reference/executors/EventLoopExecutor.html#async_anywhere.executors.EventLoopExecutor), a `concurrent.futures.Executor` which runs **`async`** code 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.
|
|
19
|
+
- [`StateTracker`](https://async-anywhere.readthedocs.io/en/latest/reference/state_tracker/StateTracker.html#async_anywhere.state_tracker.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 an `async`- and thread-agnostic way.
|
|
20
|
+
- Several other tools and utilities related to Python concurrency management.
|
|
21
|
+
|
|
22
|
+
See "Examples" below, or [the API documentation](https://async-anywhere.readthedocs.io/en/latest/reference/index.html) for more information.
|
|
23
|
+
|
|
24
|
+
## Installation and Requirements
|
|
25
|
+
|
|
26
|
+
This module requires Python 3.13.3+, and can be installed from [PyPI](https://pypi.org/project/async-anywhere/), with `pip`:
|
|
27
|
+
|
|
28
|
+
```console
|
|
29
|
+
pip install async-anywhere
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or via `uv`:
|
|
33
|
+
|
|
34
|
+
```console
|
|
35
|
+
uv add async-anywhere
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`async-anywhere` is a pure-Python module with no required compiled components, and aims to have as few pure-python transitive dependencies as possible.
|
|
39
|
+
|
|
40
|
+
## Examples
|
|
41
|
+
|
|
42
|
+
### `AwaitableFuture`
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from async_anywhere.futures import AwaitableFuture
|
|
46
|
+
from threading import Thread
|
|
47
|
+
import asyncio
|
|
48
|
+
|
|
49
|
+
loop = asyncio.new_event_loop()
|
|
50
|
+
fut = AwaitableFuture(
|
|
51
|
+
# Don't allow calls to .result() if this loop is running; that'll deadlock.
|
|
52
|
+
prevent_blocking_wait_within=[loop],
|
|
53
|
+
)
|
|
54
|
+
fut.add_done_callback(lambda f: print(f"Future {f} is done!"), run_in_forked_child=True)
|
|
55
|
+
|
|
56
|
+
async def foo():
|
|
57
|
+
print(await fut)
|
|
58
|
+
|
|
59
|
+
# Wait from one event loop:
|
|
60
|
+
Thread(target=asyncio.run, args=(foo(),)).start()
|
|
61
|
+
# ... two event loops:
|
|
62
|
+
Thread(target=asyncio.run, args=(foo(),)).start()
|
|
63
|
+
# Set the result from anywhere:
|
|
64
|
+
Thread(target=fut.set_result, args=("All done!",)).start()
|
|
65
|
+
# Wait from event loops without a wrapper:
|
|
66
|
+
print(loop.run_until_complete(fut))
|
|
67
|
+
# ... or synchronously:
|
|
68
|
+
print(fut.result(timeout=1))
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### `EventLoopExecutor`
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from async_anywhere.executors import EventLoopExecutor
|
|
75
|
+
import asyncio
|
|
76
|
+
|
|
77
|
+
executor = EventLoopExecutor(idle_timeout=10) # Dies if not given work to do for 10 seconds
|
|
78
|
+
|
|
79
|
+
fut = executor.submit(asyncio.sleep, 1, result="Hello from a captive loop after 1 second!")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@executor.bridge
|
|
83
|
+
async def runs_in_captive_loop_thread():
|
|
84
|
+
print("Hello from a captive event loop on a dedicated thread!")
|
|
85
|
+
print(await executor.submit(asyncio.sleep, 1, result="Called from inside the executor!"))
|
|
86
|
+
print(await fut) # Await futures from anywhere
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
async def wait_for_executor():
|
|
90
|
+
await runs_in_captive_loop_thread() # Wait for results from async code
|
|
91
|
+
print(await fut) # Wait from futures from any loop/thread
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
asyncio.run(wait_for_executor())
|
|
95
|
+
print(fut.result(timeout=2)) # ...or from blocking code.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# Wait for executors to shut down from async code
|
|
99
|
+
async def finish():
|
|
100
|
+
await executor.shutdown(wait=False, cancel_futures=True)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
# ...or synchronous code
|
|
104
|
+
executor.shutdown(wait=True, cancel_futures=True).result()
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`EventLoopExecutor` is ideal for integrating third-party `async` code that wants to own the event loop/entry point (e.g. [`discordpy`](https://discordpy.readthedocs.io/en/stable/quickstart.html)) 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.
|
|
109
|
+
|
|
110
|
+
### Compatibility
|
|
111
|
+
|
|
112
|
+
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.
|
|
113
|
+
|
|
114
|
+
`async-anywhere` supports:
|
|
115
|
+
|
|
116
|
+
- Code written for `asyncio`.
|
|
117
|
+
- Code written for `trio`, though Trio support is a bit fussy to use and will be improved in a future release.
|
|
118
|
+
- Code written for `anyio`.
|
|
119
|
+
- Code using `AbstractEventLoop`, including `uvloop`.
|
|
120
|
+
- Correct, minimally locking operations with or without the Global Interpreter Lock (GIL).
|
|
121
|
+
|
|
122
|
+
Code using [nest-asyncio](https://pypi.org/project/nest-asyncio/), or other tools which monkeypatch or mock out core `asyncio` primitives, may not work well with this library.
|
|
123
|
+
|
|
124
|
+
## When should you use this module?
|
|
125
|
+
|
|
126
|
+
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.
|
|
127
|
+
|
|
128
|
+
- `EventLoopExecutor` is the most powerful tool in this module, and is useful when you need to invoke `async` code from a combination of synchronous code, multiple threads, and multiple different event loops, and `asgiref.async_to_sync` isn't sufficient ([when to use `EventLoopExecutor`](https://async-anywhere.readthedocs.io/en/latest/reference/executors/when_to_use.html)).
|
|
129
|
+
- `AwaitableFuture` is useful if you have code which heavily uses `asyncio.Future` or `concurrent.futures.Future` directly, and you want a friendlier (and more performant in many cases) API ([when to use `AwaitableFuture`](https://async-anywhere.readthedocs.io/en/latest/reference/futures/when_to_use.html)).
|
|
130
|
+
- `StateTracker` is 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 use `StateTracker`](https://async-anywhere.readthedocs.io/en/latest/reference/state_tracker/when_to_use.html)).
|
|
131
|
+
|
|
132
|
+
For how `EventLoopExecutor` compares to other tools that bridge synchronous and `async` code, see [Prior Art](https://async-anywhere.readthedocs.io/en/latest/reference/executors/prior_art.html).
|
|
133
|
+
|
|
134
|
+
## Fork Safety
|
|
135
|
+
|
|
136
|
+
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()`:
|
|
137
|
+
|
|
138
|
+
- [`EventLoopExecutor` fork safety](https://async-anywhere.readthedocs.io/en/latest/reference/executors/fork_safety.html).
|
|
139
|
+
- [`AwaitableFuture` fork safety](https://async-anywhere.readthedocs.io/en/latest/reference/futures/fork_safety.html).
|
|
140
|
+
- [`StateTracker` fork safety](https://async-anywhere.readthedocs.io/en/latest/reference/state_tracker/fork_safety.html).
|
|
141
|
+
|
|
142
|
+
**Note** that care is still needed when forking in the presence of this module or concurrent Python code generally; read
|
|
143
|
+
the above carefully in order to understand tradeoffs related to `fork(2)`.
|
|
144
|
+
|
|
145
|
+
## Development and Contributing
|
|
146
|
+
|
|
147
|
+
See [CONTRIBUTING.md](https://github.com/zbentley/async-anywhere/blob/main/CONTRIBUTING.md)
|
|
148
|
+
|
|
149
|
+
### LLM usage
|
|
150
|
+
|
|
151
|
+
Code in this module was written by hand. LLMs are sometimes used by maintainers to identify issues, prototype changes, or write tests.
|
|
152
|
+
|
|
153
|
+
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.
|
|
154
|
+
|
|
155
|
+
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.
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
`async-anywhere` is free and open source software. See [`LICENSE`](https://github.com/zbentley/async-anywhere/blob/main/LICENSE) for more information.
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "flit_core.buildapi"
|
|
3
|
+
requires = ["flit_core >=4,<5"]
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
authors = [
|
|
7
|
+
{name = "Zac Bentley", email = "zbentley@users.noreply.github.com"},
|
|
8
|
+
]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 3 - Alpha",
|
|
11
|
+
"Framework :: AsyncIO",
|
|
12
|
+
"Intended Audience :: Developers",
|
|
13
|
+
"Operating System :: MacOS",
|
|
14
|
+
"Operating System :: POSIX :: Linux",
|
|
15
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Programming Language :: Python :: 3.14",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"attrs>=23.2.0",
|
|
22
|
+
"sniffio>=1.3.0,<2.0",
|
|
23
|
+
]
|
|
24
|
+
description = "Interact with your async code from any thread or event loop"
|
|
25
|
+
dynamic = ["version"]
|
|
26
|
+
license = "MIT"
|
|
27
|
+
license-files = ["LICENSE"]
|
|
28
|
+
name = "async_anywhere"
|
|
29
|
+
readme = "README.md"
|
|
30
|
+
requires-python = ">=3.13.3"
|
|
31
|
+
|
|
32
|
+
[dependency-groups]
|
|
33
|
+
dev = [
|
|
34
|
+
"anyio>=4.15.1",
|
|
35
|
+
"asgiref>=3.12.1",
|
|
36
|
+
"black>=26.10.1",
|
|
37
|
+
"coverage[toml]>=7.16.2",
|
|
38
|
+
"deptry>=0.25.1",
|
|
39
|
+
"furo>=2025.12.19",
|
|
40
|
+
"myst-parser>=5.1.0",
|
|
41
|
+
"packaging>=26.3",
|
|
42
|
+
"pip>=26.2.1",
|
|
43
|
+
"Pygments>=2.21.0",
|
|
44
|
+
"pyright>=1.1.414",
|
|
45
|
+
"pytest>=9.1.1",
|
|
46
|
+
"pytest-clarity>=1.0.1",
|
|
47
|
+
"pytest-cov>=7.1.0",
|
|
48
|
+
"pytest-doctestplus>=1.7.1",
|
|
49
|
+
"pytest-icdiff>=0.9",
|
|
50
|
+
"pytest-repeat>=0.9.4",
|
|
51
|
+
"pytest-rerunfailures>=16.7",
|
|
52
|
+
"pytest-timeout>=2.4.0",
|
|
53
|
+
"ruff>=0.17.1",
|
|
54
|
+
"sphinx>=9.1.0",
|
|
55
|
+
"sphinx-autobuild>=2025.8.25",
|
|
56
|
+
"sphinx-autodoc-typehints>=3.17.5",
|
|
57
|
+
"trio>=0.34.0",
|
|
58
|
+
"typeguard>=4.6.0",
|
|
59
|
+
"uvloop>=0.23.0",
|
|
60
|
+
"xdoctest[colors]>=1.3.2",
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
[project.urls]
|
|
64
|
+
Changelog = "https://github.com/zbentley/async-anywhere/releases"
|
|
65
|
+
Documentation = "https://async-anywhere.readthedocs.io"
|
|
66
|
+
Homepage = "https://github.com/zbentley/async-anywhere"
|
|
67
|
+
Issues = "https://github.com/zbentley/async-anywhere/issues"
|
|
68
|
+
Repository = "https://github.com/zbentley/async-anywhere"
|
|
69
|
+
Source = "https://github.com/zbentley/async-anywhere"
|
|
70
|
+
|
|
71
|
+
[tool.coverage.report]
|
|
72
|
+
fail_under = 100
|
|
73
|
+
show_missing = true
|
|
74
|
+
|
|
75
|
+
[tool.deptry]
|
|
76
|
+
experimental_namespace_package = true
|
|
77
|
+
known_first_party = ["async_anywhere"]
|
|
78
|
+
|
|
79
|
+
[tool.pytest.ini_options]
|
|
80
|
+
anyio_mode = "auto" # Every async test runs through anyio's plugin, on the backends tests/conftest.py gives it
|
|
81
|
+
addopts = [
|
|
82
|
+
"-vvx",
|
|
83
|
+
"--strict-markers",
|
|
84
|
+
"--full-trace",
|
|
85
|
+
]
|
|
86
|
+
testpaths = ["tests"]
|
|
87
|
+
python_files = ["test_*.py"]
|
|
88
|
+
pythonpath = ["src", "."]
|
|
89
|
+
timeout = 30 # pytest-timeout: dumps every thread's stack and fails the test
|
|
90
|
+
timeout_method = "signal"
|
|
91
|
+
filterwarnings = [
|
|
92
|
+
"error",
|
|
93
|
+
"ignore:This process .* is multi-threaded:DeprecationWarning", # From os.fork(), in tests that fork
|
|
94
|
+
]
|
|
95
|
+
markers = [
|
|
96
|
+
"logs_asyncio_errors: the test expects asyncio to log errors, and checks caplog itself (see tests/conftest.py)",
|
|
97
|
+
"backends(*names): the anyio backends an async test runs on, rather than asyncio and uvloop (see tests/conftest.py)",
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
[tool.black]
|
|
101
|
+
line-length = 120
|
|
102
|
+
target-version = ["py313", "py314"]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
[tool.ruff]
|
|
106
|
+
line-length = 120
|
|
107
|
+
format.docstring-code-format = true
|
|
108
|
+
format.skip-magic-trailing-comma = false
|
|
109
|
+
|
|
110
|
+
[tool.ruff.lint]
|
|
111
|
+
extend-select = [
|
|
112
|
+
"B",
|
|
113
|
+
"B9",
|
|
114
|
+
"C",
|
|
115
|
+
"D",
|
|
116
|
+
"PL",
|
|
117
|
+
"E",
|
|
118
|
+
"F",
|
|
119
|
+
"N",
|
|
120
|
+
"I",
|
|
121
|
+
"S",
|
|
122
|
+
"W",
|
|
123
|
+
"N",
|
|
124
|
+
"UP",
|
|
125
|
+
"ASYNC",
|
|
126
|
+
"S",
|
|
127
|
+
"B",
|
|
128
|
+
"A",
|
|
129
|
+
"C4",
|
|
130
|
+
"EXE",
|
|
131
|
+
"ISC",
|
|
132
|
+
"RUF",
|
|
133
|
+
]
|
|
134
|
+
ignore = [
|
|
135
|
+
"E203",
|
|
136
|
+
"E501",
|
|
137
|
+
"ISC001",
|
|
138
|
+
"D100",
|
|
139
|
+
"D101",
|
|
140
|
+
"D102",
|
|
141
|
+
"D103",
|
|
142
|
+
"D104",
|
|
143
|
+
"D105",
|
|
144
|
+
"D106",
|
|
145
|
+
"D107",
|
|
146
|
+
"D211",
|
|
147
|
+
# "RST201","RST203","RST301",
|
|
148
|
+
# "W503",
|
|
149
|
+
"D213",
|
|
150
|
+
"S101",
|
|
151
|
+
"S606",
|
|
152
|
+
"PLR2004",
|
|
153
|
+
"UP006", # Compat with older Pythons
|
|
154
|
+
"UP032", # Compat with older Pythons
|
|
155
|
+
"S311", # Yells about all uses of random
|
|
156
|
+
"C408", # I like dict() literals, bite me
|
|
157
|
+
"PLW2901", # For-loop variable rebinding: I think the potential efficiency cost outweighs the bug risk.
|
|
158
|
+
"E731", # Sometimes assigning a lambda is the clearest way to express something.
|
|
159
|
+
"PLR0913", # Long signatures are fine
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
[tool.ruff.lint.isort]
|
|
163
|
+
combine-as-imports = true
|
|
164
|
+
lines-after-imports = 2
|
|
165
|
+
required-imports = ["from __future__ import annotations"]
|
|
166
|
+
|
|
167
|
+
[tool.ruff.lint.pydocstyle]
|
|
168
|
+
convention = "google"
|