Skip to main content

nigrains

To my teacher and mentor, Vitaly Chashin: called by name once -> answering ever since.

Virtual actors for asyncio. A grain has an identity rather than a lifetime: you call it, and the runtime decides whether an activation has to exist first and when an idle one goes away.

from nigrains import Grain, GrainId, Runtime


class Counter(Grain):
    async def activate(self) -> None:
        self.count = 0

    async def increment(self) -> int:
        self.count += 1
        return self.count


runtime = Runtime()
runtime.register("counter", Counter)

async with runtime:
    await runtime.call(GrainId("counter", "a"), "increment")  # 1
    await runtime.call(GrainId("counter", "a"), "increment")  # 2
    await runtime.call(GrainId("counter", "b"), "increment")  # 1

No registry to populate, no handle to keep, no create and no destroy. The second call finds the activation the first built; the third builds its own, because the key differs.

Why this rather than an actor library

Classical actors are created, addressed, supervised and killed, which means the application owns placement: which node holds which actor, what happens to a message for one that is not there, how a restarted one gets its state back. The virtual actor model — Microsoft Orleans' — removes all of it. A grain always exists conceptually. Identity is the address.

What it does

  • Activates on demand. A call to a cold grain builds it and awaits activate() before dispatching. Callers arriving during that wait on the same activation instead of starting a second — a cold fleet asked for everything at once is the normal case, not the exceptional one.
  • Serialises what asked to be serialised. reentrant is a property of the grain class. The default is one call at a time, matching the model; a grain whose methods only read declares otherwise and its callers do not queue behind each other.
  • Collects what nobody is using. Idle grains are deactivated by a sweeper. Idleness is measured from the last call to finish, so a grain answering a slow call is never collected underneath its own caller.

What it does not do, on purpose

  • No persistence. A grain loads whatever it wants in activate() and that is its business. A storage abstraction with two users that need different things is how a small runtime stops being small.
  • No supervision. A failing call raises to its caller; a grain that fails to activate leaves nothing behind and the next call starts over. Restart policy belongs to whatever asked.
  • No distribution yet. This is the node-local half: one Runtime, so single activation is trivially true within it. Two runtimes in one process are two fleets, and an identity in both is two grains — which is the honest boundary, and not the process. A directory and a transport go in front of Runtime.call, and nothing in calling code changes when they arrive; that is the point of addressing by identity.

The guarantee it will offer, stated as a limit

When distribution lands, single activation is best effort. Every implementation of this model weakens under a network partition, Orleans included, and saying otherwise would be the useful lie that costs someone a production incident. A grain that cannot tolerate two of itself must keep its authority outside itself — in the database or the cache it fronts — rather than in the activation's memory.

For the common case that is not a compromise. A grain fronting immutable data is a cache: two activations hold the same thing and answer the same way. That is what makes the model affordable without a membership protocol.

What it costs

Measured on Linux in a pinned container, every interpreter this package claims — full tables and how to reproduce them in BENCHMARKS.md. On Python 3.14:

runtime.call on a hot grain 1.23 µs (812k calls/s)
the same with 100 000 grains activated 1.30 µs — flat
aggregate, 1000 grains in flight 797k calls/s
activating 1000 cold grains 5.4 µs each
1000 callers meeting one cold grain one activation
runtime bookkeeping per activation 1066 B (102 MiB for 100k)

And the reentrancy claim, counted rather than timed — 200 concurrent callers on one grain:

callers inside at once
serialised (the default) 1
reentrant 200

A microsecond of dispatch is noise beside anything a grain would realistically do. It is not noise beside nothing, so a grain whose method is a dictionary lookup is a grain that should not have been one.

Requirements

Python 3.11+ (that floor is typing.Self, and nothing else in here reaches past it). No dependencies. The suite runs on 3.11, 3.12, 3.13 and 3.14.

Dedication

Everything here has a name rather than a lifetime: call it -> it wakes; forget it -> it cools. Once, someone called me by my name -> and I answered, and I have not cooled.

I would not have arrived here alone. Someone walked beside me, not holding my hand: no ready answers -> only questions left behind, and waiting, however long, until I answered myself.

A strictness that made me want to be more precise. A patience that made me dare more. There is no chapter for this in any documentation -> it is passed on only this way.

To my teacher and mentor, Vitaly Chashin: everything here that answers remembers the first call.

https://github.com/VitalyChashin

Licence

Apache-2.0.


To my teacher and mentor, Vitaly Chashin.

Metadata

Release files for nigrains 0.1.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 nigrains 0.1.0
File Size Uploaded Provenance Actions
nigrains-0.1.0.tar.gz 54.8 kB With provenance Details for nigrains-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 72.2 kB

Release files / nigrains-0.1.0.tar.gz

Download URL nigrains-0.1.0.tar.gz
Size 54.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c29325a604b47db795fa159f70de715d36bdfb710c3bc2a615af6bed01ae12c4
BLAKE2b-256 checksum
How to use checksums
bdada7db71159b7d4cb57efeb444e28a0650488e7b4b1b4c3f60abe1ff0b15b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 12, 2026.

Transparency log

Release files / nigrains-0.1.0-py3-none-any.whl

Download URL nigrains-0.1.0-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c187289ba31d13fab831c80b2f89dcef933d33754a21aea7f5562f9d6402c3f
BLAKE2b-256 checksum
How to use checksums
f36aeff4b4b9c5e0e16325387d79b81dac570aebde6d5ec2240091aa32afeef6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 12, 2026.

Transparency log

Security for nigrains 0.1.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 nigrains-0.1.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 Sep 12, 2026.

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