-
Notifications
You must be signed in to change notification settings - Fork 36
Ctx context manager and ContextVar #563
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
Conversation
|
I like this very much for usage in the test blocks. I have an old prototype of this on my "post Python 3" list, so thanks! However, I'm a bit hesitant about encouraging it as a general pattern, because contexts should be created rarely. Contexts are relatively expensive: each one has a separate thread pool and cache, and recreating a context in an interactive session could be detrimental to performance (because the caches will be invalidated). The pattern I'm worried about is: I also think the |
|
Relatedly: I believe the only reason I restricted updating |
Presumably one would(should) use this only if the second context uses a different config from the first, in which case the extra cost of a new
No strong opinion on naming, |
ihnorton
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinion on naming, scope_ctx is fine
I think making the distinction from Ctx is sufficient for my other usage concern. Please make this change, and otherwise this LGTM (pending any refactoring to use it internally). Thank you!
…ope_ctx() context manager
ihnorton
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 thanks!
Problem description
Most
tiledbfunctions and classes accept an optionalctxparameter. This is less than ideal for the following reasons:Taking examples/string_float_int_dimensions.py as an example,
ctxis initialized and passed to 6 different calls:Error-prone
In the example above
ctxis not passed to thetiledb.SparseArray.createcall, most likely an accidental omission. In this particular example it doesn't matter because the default globalCtxinstance is equal totiledb.Ctx()but it could matter ifctxwas initialized with a differentConfig. Such accidental omissions happen in several places in tiledb.dataframe_.Global variable
A global
Ctxinstance can be initialized and used by default viatiledb.default_ctx(). This solves the above verbosity and error-proneness issues at the cost of introducing a global variable. In addition to the usual drawbacks of global variables, the globalCtxcan be initialized only once in a program's lifetime.Proposal
This PR introduces the following enhancements:
tiledb.ctx(config=None)context manager for specifying atiledb.Ctxwithin a block of code. ThisCtxis both returned by the context manager (for explicit usage) and set as "global" within the scope of the block (for implicit usage).ContextVarcan be thought of as a generalization of global or thread-local variables that also plays well with asynchronous (async/await) code.tiledb.initialize_ctxfunction.Once/if this is accepted, a following PR will clean up the redundant and/or verbose explicit
ctxusage with appropriatetiledb.ctx()context blocks.