nigrains 0.1.0__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.
- nigrains-0.1.0/.dockerignore +7 -0
- nigrains-0.1.0/.gitattributes +2 -0
- nigrains-0.1.0/.github/workflows/check.yml +31 -0
- nigrains-0.1.0/.github/workflows/publish.yml +26 -0
- nigrains-0.1.0/.gitignore +8 -0
- nigrains-0.1.0/BENCHMARKS.md +100 -0
- nigrains-0.1.0/CHANGELOG.md +30 -0
- nigrains-0.1.0/LICENSE +202 -0
- nigrains-0.1.0/PKG-INFO +168 -0
- nigrains-0.1.0/README.md +144 -0
- nigrains-0.1.0/bench/Dockerfile +24 -0
- nigrains-0.1.0/bench/bench.py +464 -0
- nigrains-0.1.0/bench/run.sh +18 -0
- nigrains-0.1.0/pyproject.toml +79 -0
- nigrains-0.1.0/src/nigrains/__init__.py +36 -0
- nigrains-0.1.0/src/nigrains/errors.py +60 -0
- nigrains-0.1.0/src/nigrains/grain.py +100 -0
- nigrains-0.1.0/src/nigrains/py.typed +0 -0
- nigrains-0.1.0/src/nigrains/runtime.py +415 -0
- nigrains-0.1.0/tests/test_readme_example.py +30 -0
- nigrains-0.1.0/tests/test_runtime.py +471 -0
- nigrains-0.1.0/uv.lock +396 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Every version the package claims in `requires-python`, on every push.
|
|
2
|
+
# The claim is cheap to make and this is what makes it true.
|
|
3
|
+
name: check
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
branches: [main]
|
|
8
|
+
pull_request:
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
fail-fast: false
|
|
15
|
+
matrix:
|
|
16
|
+
python: ["3.11", "3.12", "3.13", "3.14"]
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
- uses: astral-sh/setup-uv@v5
|
|
20
|
+
with:
|
|
21
|
+
enable-cache: true
|
|
22
|
+
- run: uv run --python ${{ matrix.python }} pytest
|
|
23
|
+
|
|
24
|
+
lint:
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
steps:
|
|
27
|
+
- uses: actions/checkout@v4
|
|
28
|
+
- uses: astral-sh/setup-uv@v5
|
|
29
|
+
- run: uv run ruff check .
|
|
30
|
+
- run: uv run ruff format --check .
|
|
31
|
+
- run: uv run mypy src tests
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Publishes to PyPI on a tag, with no token anywhere.
|
|
2
|
+
#
|
|
3
|
+
# Trusted Publishing: PyPI verifies a short-lived OIDC token minted by this
|
|
4
|
+
# workflow instead of a secret we would have to store, rotate and eventually
|
|
5
|
+
# leak. It has to be configured once on PyPI as a pending publisher for the
|
|
6
|
+
# project name, naming this repository and this workflow file.
|
|
7
|
+
name: publish
|
|
8
|
+
|
|
9
|
+
on:
|
|
10
|
+
push:
|
|
11
|
+
tags: ["v*"]
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
publish:
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
environment: pypi
|
|
17
|
+
permissions:
|
|
18
|
+
id-token: write
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v4
|
|
21
|
+
- uses: astral-sh/setup-uv@v5
|
|
22
|
+
# The tests run again here rather than trusting that the tag points at
|
|
23
|
+
# a commit `check` already passed: a tag can be moved.
|
|
24
|
+
- run: uv run pytest
|
|
25
|
+
- run: uv build
|
|
26
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Benchmarks
|
|
2
|
+
|
|
3
|
+
Run them yourself: `bench/run.sh` builds a container and measures on every
|
|
4
|
+
interpreter this package claims. Two CPUs and 2 GiB, pinned, because a
|
|
5
|
+
benchmark whose numbers move with whatever else the machine is doing measures
|
|
6
|
+
the machine.
|
|
7
|
+
|
|
8
|
+
Everything below is Linux (Debian bookworm-slim, container, 2 CPUs). Each
|
|
9
|
+
timing is **the fastest of several runs**, not a mean: a slower run measures
|
|
10
|
+
whatever else the machine was doing at the time, and the question here is
|
|
11
|
+
what the code costs when nothing is in its way. Read the shapes, not the
|
|
12
|
+
digits — the digits are this machine's.
|
|
13
|
+
|
|
14
|
+
## Dispatch
|
|
15
|
+
|
|
16
|
+
What addressing by identity costs, against a plain `await grain.method()` on
|
|
17
|
+
a grain you already hold.
|
|
18
|
+
|
|
19
|
+
| Python | direct | `runtime.call` | throughput |
|
|
20
|
+
|---|---|---|---|
|
|
21
|
+
| 3.11 | 0.12 µs | 1.70 µs | 588k calls/s |
|
|
22
|
+
| 3.12 | 0.09 µs | 1.42 µs | 706k calls/s |
|
|
23
|
+
| 3.13 | 0.08 µs | 1.32 µs | 756k calls/s |
|
|
24
|
+
| 3.14 | 0.08 µs | 1.23 µs | **812k calls/s** |
|
|
25
|
+
|
|
26
|
+
A little over a microsecond, which is roughly one event-loop turn. That is
|
|
27
|
+
noise beside anything a grain would realistically do — a query, a file, a
|
|
28
|
+
model call — and it is not noise beside nothing. A grain whose method is a
|
|
29
|
+
dictionary lookup should not have been a grain.
|
|
30
|
+
|
|
31
|
+
**Dispatch does not degrade with the size of the fleet**, which is the
|
|
32
|
+
premise of the whole model and therefore the thing most worth checking:
|
|
33
|
+
|
|
34
|
+
| activated grains | 3.14 |
|
|
35
|
+
|---|---|
|
|
36
|
+
| 1 | 1.27 µs |
|
|
37
|
+
| 1 000 | 1.29 µs |
|
|
38
|
+
| 100 000 | 1.30 µs |
|
|
39
|
+
|
|
40
|
+
Aggregate over a whole fleet in flight at once — 1000 grains, 100 calls each
|
|
41
|
+
— is 797k calls/s on 3.14, so nothing is lost by spreading the work out.
|
|
42
|
+
|
|
43
|
+
## Overlap
|
|
44
|
+
|
|
45
|
+
200 concurrent callers on one grain. Counted, not timed: the number is how
|
|
46
|
+
many were inside the method at the same moment.
|
|
47
|
+
|
|
48
|
+
| | callers inside at once |
|
|
49
|
+
|---|---|
|
|
50
|
+
| serialised (the default) | **1** |
|
|
51
|
+
| reentrant | **200** |
|
|
52
|
+
|
|
53
|
+
## Activation
|
|
54
|
+
|
|
55
|
+
| | 3.11 | 3.14 |
|
|
56
|
+
|---|---|---|
|
|
57
|
+
| 1000 cold grains, called at once | 8.10 µs each | **5.42 µs each** |
|
|
58
|
+
| 1000 callers meeting one cold grain | one activation | one activation |
|
|
59
|
+
| runtime bookkeeping per activation | 1126 B | **1066 B** |
|
|
60
|
+
|
|
61
|
+
A hundred thousand activated grains cost about **102 MiB** of the runtime's
|
|
62
|
+
own structures, before a grain holds anything of its own. That is the number
|
|
63
|
+
to start from when asking how many one node can keep.
|
|
64
|
+
|
|
65
|
+
**A grain still activating is never collected**, however long it has been
|
|
66
|
+
sitting there — its call count is zero and its timestamp is the moment it was
|
|
67
|
+
created, which used to look exactly like abandonment. A review found the
|
|
68
|
+
sweep deactivating a grain whose own first caller was still waiting for it.
|
|
69
|
+
|
|
70
|
+
## Sweeping idle grains
|
|
71
|
+
|
|
72
|
+
The sweep is the one operation linear in the size of the fleet. It is chunked,
|
|
73
|
+
and the two numbers say why:
|
|
74
|
+
|
|
75
|
+
| 100 000 idle grains, 3.14 | total | longest stall |
|
|
76
|
+
|---|---|---|
|
|
77
|
+
| in one go | 81.0 ms | **81.1 ms** |
|
|
78
|
+
| chunked (current) | 83.6 ms | **6.9 ms** |
|
|
79
|
+
|
|
80
|
+
The total is what nobody waits for. The stall is what a caller waits — one
|
|
81
|
+
uninterrupted block with every other coroutine behind it. Chunking trades a
|
|
82
|
+
few per cent of total time for an order of magnitude off the thing anybody
|
|
83
|
+
actually feels.
|
|
84
|
+
|
|
85
|
+
## Two ways this benchmark was wrong first
|
|
86
|
+
|
|
87
|
+
Both are kept here because the next person to measure this will reach for the
|
|
88
|
+
same tools.
|
|
89
|
+
|
|
90
|
+
**Overlap was measured with `asyncio.sleep(0.01)` and wall time.** On Windows
|
|
91
|
+
the default timer resolution is about 15.6 ms, so a 10 ms sleep took one and a
|
|
92
|
+
half ticks and the serialised grain scored *0.6x* — slower than serial, which
|
|
93
|
+
is meaningless. The number was about the platform's clock. Counting callers
|
|
94
|
+
costs the same everywhere.
|
|
95
|
+
|
|
96
|
+
**Memory per activation counted the tasks that made the activations.** The
|
|
97
|
+
reading was taken right after a `gather` of 100 000 calls, before anything
|
|
98
|
+
collected them, which put finished coroutines into the cost of a grain. One
|
|
99
|
+
`gc.collect()` moved it from 1228 to 1058 bytes, and only the second number is
|
|
100
|
+
about this package.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 — 2026-09-07
|
|
4
|
+
|
|
5
|
+
First cut: the node-local half of the runtime.
|
|
6
|
+
|
|
7
|
+
- `Grain`, `GrainId` and `Runtime`: activation on demand by identity,
|
|
8
|
+
deactivation of idle grains, reentrancy declared per grain class.
|
|
9
|
+
- Callers meeting a cold grain wait on one activation rather than starting
|
|
10
|
+
several.
|
|
11
|
+
- A failed activation leaves nothing behind and reaches every waiting caller.
|
|
12
|
+
- Idleness is measured from the last call to finish, so a grain answering a
|
|
13
|
+
slow call is never collected underneath its caller.
|
|
14
|
+
- The idle sweep yields every thousand grains and confirms idleness again
|
|
15
|
+
before deactivating, so collecting a large fleet is not one long stall:
|
|
16
|
+
100 000 idle grains went from an 79 ms block to a 6 ms longest pause.
|
|
17
|
+
- An activation keeps neither its resolved activation event nor a lock it
|
|
18
|
+
has no use for; both are made or dropped when they stop mattering, which
|
|
19
|
+
is 1066 bytes of runtime bookkeeping per grain rather than 1228.
|
|
20
|
+
- Callers waiting on a cold grain wait on an Event rather than on a shared
|
|
21
|
+
future, so one of them being cancelled no longer cancels the activation
|
|
22
|
+
everybody else was waiting for. A caller whose activation was cancelled by
|
|
23
|
+
somebody else's task retries rather than inheriting the cancellation.
|
|
24
|
+
- A grain that has not finished activating is never treated as idle, so an
|
|
25
|
+
`activate()` slower than the idle span is no longer indistinguishable from
|
|
26
|
+
abandonment.
|
|
27
|
+
|
|
28
|
+
Not here yet, and named in the README rather than implied away: persistence,
|
|
29
|
+
supervision, and distribution. Single activation is trivially true while
|
|
30
|
+
there is one node, and will be best effort when there is not.
|
nigrains-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
nigrains-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: nigrains
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Virtual actors for asyncio: grains addressed by identity, activated on demand.
|
|
5
|
+
Project-URL: Homepage, https://github.com/yadozub/nigrains
|
|
6
|
+
Project-URL: Source, https://github.com/yadozub/nigrains
|
|
7
|
+
Project-URL: Issues, https://github.com/yadozub/nigrains/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/yadozub/nigrains/blob/main/CHANGELOG.md
|
|
9
|
+
Author: Sergey Ilyevich
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: actors,asyncio,grains,orleans,virtual-actors
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Framework :: AsyncIO
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# nigrains
|
|
26
|
+
|
|
27
|
+
*To my teacher and mentor, [Vitaly Chashin](https://github.com/VitalyChashin): called by name once -> answering ever since.*
|
|
28
|
+
|
|
29
|
+
Virtual actors for asyncio. A grain has an identity rather than a lifetime:
|
|
30
|
+
you call it, and the runtime decides whether an activation has to exist
|
|
31
|
+
first and when an idle one goes away.
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from nigrains import Grain, GrainId, Runtime
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Counter(Grain):
|
|
38
|
+
async def activate(self) -> None:
|
|
39
|
+
self.count = 0
|
|
40
|
+
|
|
41
|
+
async def increment(self) -> int:
|
|
42
|
+
self.count += 1
|
|
43
|
+
return self.count
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
runtime = Runtime()
|
|
47
|
+
runtime.register("counter", Counter)
|
|
48
|
+
|
|
49
|
+
async with runtime:
|
|
50
|
+
await runtime.call(GrainId("counter", "a"), "increment") # 1
|
|
51
|
+
await runtime.call(GrainId("counter", "a"), "increment") # 2
|
|
52
|
+
await runtime.call(GrainId("counter", "b"), "increment") # 1
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
No registry to populate, no handle to keep, no create and no destroy. The
|
|
56
|
+
second call finds the activation the first built; the third builds its own,
|
|
57
|
+
because the key differs.
|
|
58
|
+
|
|
59
|
+
## Why this rather than an actor library
|
|
60
|
+
|
|
61
|
+
Classical actors are created, addressed, supervised and killed, which means
|
|
62
|
+
the application owns placement: which node holds which actor, what happens
|
|
63
|
+
to a message for one that is not there, how a restarted one gets its state
|
|
64
|
+
back. The virtual actor model — Microsoft Orleans' — removes all of it. A
|
|
65
|
+
grain always exists conceptually. Identity *is* the address.
|
|
66
|
+
|
|
67
|
+
## What it does
|
|
68
|
+
|
|
69
|
+
- **Activates on demand.** A call to a cold grain builds it and awaits
|
|
70
|
+
`activate()` before dispatching. Callers arriving during that wait on the
|
|
71
|
+
same activation instead of starting a second — a cold fleet asked for
|
|
72
|
+
everything at once is the normal case, not the exceptional one.
|
|
73
|
+
- **Serialises what asked to be serialised.** `reentrant` is a property of
|
|
74
|
+
the grain class. The default is one call at a time, matching the model;
|
|
75
|
+
a grain whose methods only read declares otherwise and its callers do not
|
|
76
|
+
queue behind each other.
|
|
77
|
+
- **Collects what nobody is using.** Idle grains are deactivated by a
|
|
78
|
+
sweeper. Idleness is measured from the last call to *finish*, so a grain
|
|
79
|
+
answering a slow call is never collected underneath its own caller.
|
|
80
|
+
|
|
81
|
+
## What it does not do, on purpose
|
|
82
|
+
|
|
83
|
+
- **No persistence.** A grain loads whatever it wants in `activate()` and
|
|
84
|
+
that is its business. A storage abstraction with two users that need
|
|
85
|
+
different things is how a small runtime stops being small.
|
|
86
|
+
- **No supervision.** A failing call raises to its caller; a grain that
|
|
87
|
+
fails to activate leaves nothing behind and the next call starts over.
|
|
88
|
+
Restart policy belongs to whatever asked.
|
|
89
|
+
- **No distribution yet.** This is the node-local half: **one `Runtime`**,
|
|
90
|
+
so single activation is trivially true within it. Two runtimes in one
|
|
91
|
+
process are two fleets, and an identity in both is two grains — which is
|
|
92
|
+
the honest boundary, and not the process. A directory and a transport go
|
|
93
|
+
in front of `Runtime.call`, and nothing in calling code changes when they
|
|
94
|
+
arrive; that is the point of addressing by identity.
|
|
95
|
+
|
|
96
|
+
## The guarantee it will offer, stated as a limit
|
|
97
|
+
|
|
98
|
+
When distribution lands, single activation is **best effort**. Every
|
|
99
|
+
implementation of this model weakens under a network partition, Orleans
|
|
100
|
+
included, and saying otherwise would be the useful lie that costs someone a
|
|
101
|
+
production incident. A grain that cannot tolerate two of itself must keep
|
|
102
|
+
its authority outside itself — in the database or the cache it fronts —
|
|
103
|
+
rather than in the activation's memory.
|
|
104
|
+
|
|
105
|
+
For the common case that is not a compromise. A grain fronting immutable
|
|
106
|
+
data is a cache: two activations hold the same thing and answer the same
|
|
107
|
+
way. That is what makes the model affordable without a membership protocol.
|
|
108
|
+
|
|
109
|
+
## What it costs
|
|
110
|
+
|
|
111
|
+
Measured on Linux in a pinned container, every interpreter this package
|
|
112
|
+
claims — full tables and how to reproduce them in
|
|
113
|
+
[`BENCHMARKS.md`](BENCHMARKS.md). On Python 3.14:
|
|
114
|
+
|
|
115
|
+
| | |
|
|
116
|
+
|---|---|
|
|
117
|
+
| `runtime.call` on a hot grain | **1.23 µs** (812k calls/s) |
|
|
118
|
+
| the same with 100 000 grains activated | **1.30 µs** — flat |
|
|
119
|
+
| aggregate, 1000 grains in flight | 797k calls/s |
|
|
120
|
+
| activating 1000 cold grains | 5.4 µs each |
|
|
121
|
+
| 1000 callers meeting one cold grain | **one** activation |
|
|
122
|
+
| runtime bookkeeping per activation | 1066 B (102 MiB for 100k) |
|
|
123
|
+
|
|
124
|
+
And the reentrancy claim, counted rather than timed — 200 concurrent callers
|
|
125
|
+
on one grain:
|
|
126
|
+
|
|
127
|
+
| | callers inside at once |
|
|
128
|
+
|---|---|
|
|
129
|
+
| serialised (the default) | **1** |
|
|
130
|
+
| reentrant | **200** |
|
|
131
|
+
|
|
132
|
+
A microsecond of dispatch is noise beside anything a grain would realistically
|
|
133
|
+
do. It is not noise beside nothing, so a grain whose method is a dictionary
|
|
134
|
+
lookup is a grain that should not have been one.
|
|
135
|
+
|
|
136
|
+
## Requirements
|
|
137
|
+
|
|
138
|
+
Python 3.11+ (that floor is `typing.Self`, and nothing else in here reaches
|
|
139
|
+
past it). No dependencies. The suite runs on 3.11, 3.12, 3.13 and 3.14.
|
|
140
|
+
|
|
141
|
+
## Dedication
|
|
142
|
+
|
|
143
|
+
Everything here has a name rather than a lifetime:
|
|
144
|
+
call it -> it wakes; forget it -> it cools.
|
|
145
|
+
Once, someone called me by my name ->
|
|
146
|
+
and I answered, and I have not cooled.
|
|
147
|
+
|
|
148
|
+
I would not have arrived here alone.
|
|
149
|
+
Someone walked beside me, not holding my hand:
|
|
150
|
+
no ready answers -> only questions left behind,
|
|
151
|
+
and waiting, however long, until I answered myself.
|
|
152
|
+
|
|
153
|
+
A strictness that made me want to be more precise.
|
|
154
|
+
A patience that made me dare more.
|
|
155
|
+
There is no chapter for this in any documentation -> it is passed on only this way.
|
|
156
|
+
|
|
157
|
+
To my teacher and mentor, Vitaly Chashin:
|
|
158
|
+
everything here that answers remembers the first call.
|
|
159
|
+
|
|
160
|
+
<https://github.com/VitalyChashin>
|
|
161
|
+
|
|
162
|
+
## Licence
|
|
163
|
+
|
|
164
|
+
Apache-2.0.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
*To my teacher and mentor, Vitaly Chashin.*
|