Comments (7)
In terms of passing down values to subactors as is done with the statespace
arg right now I think a better name for this is simply vars
to pair with ContextVar
?
from tractor.
Re-thinking this after delving into contextvars
a little more... what we're actually after is a per-process (aka actor) variable scope that can be accessed from all the tasks running in that process. What would be handy is it keeping knowledge about which task did what in some kind of history for debugging purposes (ex. logging, remote state tracking).
I'm thinking a ContextVar
inspired api but with a MutableMapping
nod like:
vars = tractor.LocalVars('vars')
state = vars.setdefault('actor_local_state', 10)
# ... later some other task
state = vars.get('actor_local_state')
Is fine and underneath we can use contextvars.ContextVar
(if necessary) to keep tabs on the per-task interaction but the actual data is just a global mapping type that can be modified and used to past state around. A clear use case I've come across is locally cached network clients that are ideally shared between other actors.
There should in theory be no state clobbering which would require locking since each actor is single threaded.
from tractor.
I'm almost sold on:
blah = tractor.ActorVar('blah', default=10)
stack = tractor.ContextStack('my_stack')
as module level style declarations that track actor level state and context.
For the ContextStack
I'm thinking of making this a small instance which contains both a ExitStack
and AsyncExitStack
exposing certain methods from both but allowing for flags to defer teardown when actors are run with the new debug mode enabled. This would allow for deferring teardown of resources that must be destroyed on process termination but that maybe need to be introspected from a debugger beforehand.
A ContextStack
would actually be an example of an ActorVar
which can be accessed by tasks process-wide.
I'm not entirely sure if allowing the user to define their own stack names is necessary.
In theory you probably only need one unless the user wants to have some stacks in "don't teardown till after debug" mode versus all of them all the time in the singleton actor-wide-stack case.
In the singleton choice you could probably just have a single (async) with tractor.context_stack() as stack:
style api where on the with
close the teardowns are not immediately invoked until the actor runtime is terminated thus allowing the debugger (or other crash handlers) to engage before killing inter-process resources.
from tractor.
Interestingly enough the trio.lowlevel.RunVar
is a public api though nichey according to trio
peeps.
I'm wondering if just some slight wrapping around this is suitable?
- do we need to handle the case of multiple
trio
runs in a process having separate actor state? - are there cases where ^ is not desired?
I'm also just noticing now that the stdlib's contextvars
is actually implemented in C, to there may be slight speed considerations?
from tractor.
Hmm all this use of context leads me to think we should change our own Context
an an IPCConext
or something. ChannelContext
, TransportContext
?
Also pretty sure the current_context()
in that module isn't going to work?
from tractor.
Thinking about this further, I don't think any of this api is really required from the outset
We can probably get away with just encouraging the use of plain old module level variables / globals for state "sharing" in each actor (since globals are "global" per process).
The main use for such a system would be to track state changes made by tasks which can be done already with trio.RunVar
if needed. I think maybe moving toward removing Actot.statespace
and encouraging use of both module variables and RunVar
if needed.
from tractor.
Going along in time more I'm thinking tractor.LocalVar
(or wtv) will have further value when looking at potentially moving the project towards support for repl-driven-programming.
For example if we want to make it possible to respawn the currently crashed task or whole actor from the debugger / shell, unwinding state changes may be necessary? I guess mostly in the crashed task case.
from tractor.
Related Issues (20)
- Nursery does not re-raise error from `.run_in_actor()` remote task HOT 22
- Deprecate `ActorNursery.run_in_actor()` and offer as part of a wrapper cluster API HOT 1
- PyPI packaging broken for version 0.1.0a4; missing README.rst HOT 2
- Toying with a message type for "object references" HOT 1
- Add a basic *registry* actor example
- tHe GoD uNpRoToCoL HOT 1
- Caps based sec, as much as we can with Python
- We might not need `ActorNursery._join_procs: trio.Event` in the long run
- Use of `pydantic` and `click` in sub-actor causes SIGINT ignoring, hangs...
- Macos error while trying to initialize HOT 2
- Need to handle a `KeyError` in `_debug._hijack_stdin_for_child` HOT 2
- Drop `msgpack` dependency, go full `msgspec`
- Handling >= 1 depth (nested) actor trees in the debugger
- `pdbpp` borks our `0.1.0a5` on `pypi` 😂
- Recursion error on `BroadcastReceiver.receive()`?
- `trio.Process` deprecations
- `trio.MultiError` is deprecated -> move to `exceptiongroup.ExceptionGroup` HOT 1
- Share memory (array) sub-system API and optional tight integration with `numpy`
- OS X: shared memory file name length limitation causing error
- `OSError: [Errno 9] Bad file descriptor` in cluster test
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tractor.