Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maybe_open_context() an actor wide task-resource cache #257

Merged
merged 15 commits into from
Dec 17, 2021

Conversation

goodboy
Copy link
Owner

@goodboy goodboy commented Oct 27, 2021

Like it sounds: a context manager that only opens a new actor-global (aka mem local) resource if one isn't already cached.

This is particularly handy for resources that can be shared by multiple tasks in and actor where the user desires that the lifetime of the resource is maintained until the final user task completes.

Still needs tests obviously.

UPDATE:
This ended up turning into a foray into fixing MsgStream closure/termination semantics after trying to get the original resource caching test working.

Todo tests:

  • cached inter-actor stream demonstration
  • simpler audit of just ensuring that multiple local trio tasks get the same cached value
  • error/cancel tests?

# Further potential examples of interest:
# https://gist.github.com/njsmith/cf6fc0a97f53865f2c671659c88c1798#file-cache-py-L8

class cache:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unusual to have a lower-case class name, IMO.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. do you think Cache is ok even though it's never being used as an instance?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's totally ok.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to make it _Cache to keep it internal?

@goodboy goodboy force-pushed the context_caching branch 2 times, most recently from 8c6e4ee to 35ac323 Compare November 9, 2021 02:38
@goodboy goodboy force-pushed the context_caching branch 4 times, most recently from ef5162a to 52fef7f Compare December 6, 2021 00:53
@goodboy goodboy force-pushed the context_caching branch 6 times, most recently from 0bd1be5 to 903ca53 Compare December 12, 2021 01:17
@goodboy
Copy link
Owner Author

goodboy commented Dec 15, 2021

@overclockworked64 there I think the test should explain a little better how this works.

I also fixed up the naming concern to _Cache.

@goodboy
Copy link
Owner Author

goodboy commented Dec 16, 2021

gahhhhh windowwwwws

@goodboy
Copy link
Owner Author

goodboy commented Dec 16, 2021

Lol this mypy thing..

mypy.ini:2: error: Error importing plugin "trio_typing.plugin": cannot import name 'TypeVarDef' from 'mypy.types' (/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/mypy/types.cpython-39-x86_64-linux-gnu.so)

@goodboy
Copy link
Owner Author

goodboy commented Dec 16, 2021

If the streaming improvements here seem too lumped in we can also break them out into a separate PR.

It might be better to more formally document those fixes and the new test?
I dunno wat y'all think?

After more extensive testing I realized that keying on the context
manager *instance id* isn't going to work since each entering task is
going to create a unique key XD

Instead pass the manager function as `acm_func` and optionally allow
keying the resource on the passed `kwargs` (if hashable) or the
`key:str`. Further, pass the key to the enterer task and avoid
a separate keying scheme for the manager versus the value it delivers.
Don't bother with checking and releasing the lock in `finally:` block,
it should be an error if it's still locked.
@goodboy goodboy merged commit 4001d2c into master Dec 17, 2021
@goodboy goodboy deleted the context_caching branch December 17, 2021 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants