-
Notifications
You must be signed in to change notification settings - Fork 47
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
feat: bigframes.options
and bigframes.option_context
now uses thread-local variables to prevent context managers in separate threads from affecting each other
#652
Conversation
…ead-local variables to prevent context managers in separate threads from affecting each other In our tests, this allows us to actually test things like `bf.option_context("display.repr_mode", "deferred"):` without always having some other test change the display mode and break the test. Fixes internal issue 308657813
bigframes/_config/__init__.py
Outdated
self._compute_options = compute_options.ComputeOptions() | ||
self._local.bigquery_options = None | ||
|
||
def _init_biquery_thread_local(self): |
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.
typo: bigquery
Sorry, something went wrong.
All reactions
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. Fixed in 4fa1a92
Sorry, something went wrong.
All reactions
e2e failure of |
All reactions
Sorry, something went wrong.
Cover failed.
Looks like I now have some dead code in that test. Will investigate. |
All reactions
Sorry, something went wrong.
Messaged offline. @tswast will fix the failing test, and then I will review. |
All reactions
Sorry, something went wrong.
|
All reactions
Sorry, something went wrong.
I'm able to reproduce locally. The thread local variable is working how I expect, but the test is making an assumption that the DataFrame doesn't have a I'll update the test.
|
All reactions
-
👍 1 reaction -
👀 1 reaction
Sorry, something went wrong.
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.
Just had one simplification suggestion, feel free to just close and re-request if you prefer the way it is now. Thanks!
Sorry, something went wrong.
All reactions
bigframes/_config/__init__.py
Outdated
# BigQuery options are special because they can only be set once per | ||
# session, so we need an indicator as to whether we are using the | ||
# thread-local session or the global session. | ||
self._local.is_bigquery_thread_local = False |
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.
If I'm reading this correctly, would checking for self._local.bigquery_options is None be able to replace this boolean state? It seems a little more DRY to do it that way, if so.
Sorry, something went wrong.
All reactions
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.
I like it! Done in a7f3c96
Sorry, something went wrong.
All reactions
bigframes/_config/__init__.py
Outdated
# Already thread-local, so don't reset any options that have been set | ||
# already. No locks needed since this only modifies thread-local | ||
# variables. | ||
if self._local.is_bigquery_thread_local: |
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.
i.e., this could be "if self._local.bigquery_options is not None"
Sorry, something went wrong.
All reactions
bigframes/_config/__init__.py
Outdated
|
||
@property | ||
def bigquery(self) -> bigquery_options.BigQueryOptions: | ||
"""Options to use with the BigQuery engine.""" | ||
if self._local.is_bigquery_thread_local: |
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.
Here too
Sorry, something went wrong.
All reactions
bigframes/_config/__init__.py
Outdated
|
||
This is set to True by using `option_context` with a "bigquery" option. | ||
""" | ||
return self._local.is_bigquery_thread_local |
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.
This could also check none (and the comment would need to be tweaked.)
Sorry, something went wrong.
All reactions
# sessions if we allow that. | ||
if bigframes.options.is_bigquery_thread_local: | ||
bigframes.close_session() | ||
bigframes.options._local.is_bigquery_thread_local = False |
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.
close_session could set _local.bigquery_options to None
Sorry, something went wrong.
All reactions
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.
Someone could call close_session()
inside of a context manager. We actually have some tests that start and stop multiple sessions in this thread-local context. By using the options as the thread locality indicator, we need to make sure that object stays around.
Sorry, something went wrong.
All reactions
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.
Oh, right, makes sense.
Sorry, something went wrong.
All reactions
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.
LGTM, thanks!
Sorry, something went wrong.
All reactions
milkshakeiii
junyazhang
GarrettWu
Successfully merging this pull request may close these issues.
None yet
In our tests, this allows us to actually test things like
bf.option_context("display.repr_mode", "deferred"):
without always having some other test change the display mode and break the test.Fixes internal issue 308657813
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> 🦕