Skip to content

fix: Resolve issue with unset thread-local options #741

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

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions bigframes/_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
DataFrames from this package.
"""

from __future__ import annotations

import copy
from dataclasses import dataclass, field
import threading
from typing import Optional

import bigframes_vendored.pandas._config.config as pandas_config

Expand All @@ -28,18 +32,27 @@
import bigframes._config.sampling_options as sampling_options


@dataclass
class ThreadLocalConfig(threading.local):
# If unset, global settings will be used
bigquery_options: Optional[bigquery_options.BigQueryOptions] = None
# Note: use default factory instead of default instance so each thread initializes to default values
display_options: display_options.DisplayOptions = field(
default_factory=display_options.DisplayOptions
)
sampling_options: sampling_options.SamplingOptions = field(
default_factory=sampling_options.SamplingOptions
)
compute_options: compute_options.ComputeOptions = field(
default_factory=compute_options.ComputeOptions
)


class Options:
"""Global options affecting BigQuery DataFrames behavior."""

def __init__(self):
self._local = threading.local()

# Initialize these in the property getters to make sure we do have a
# separate instance per thread.
self._local.bigquery_options = None
self._local.display_options = None
self._local.sampling_options = None
self._local.compute_options = None
self._local = ThreadLocalConfig()

# 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
Expand All @@ -61,21 +74,16 @@ def _init_bigquery_thread_local(self):
@property
def bigquery(self) -> bigquery_options.BigQueryOptions:
"""Options to use with the BigQuery engine."""
if (
bigquery_options := getattr(self._local, "bigquery_options", None)
) is not None:
if self._local.bigquery_options is not None:
# The only way we can get here is if someone called
# _init_bigquery_thread_local.
return bigquery_options
return self._local.bigquery_options

return self._bigquery_options

@property
def display(self) -> display_options.DisplayOptions:
"""Options controlling object representation."""
if self._local.display_options is None:
self._local.display_options = display_options.DisplayOptions()

return self._local.display_options

@property
Expand All @@ -88,17 +96,11 @@ def sampling(self) -> sampling_options.SamplingOptions:
matplotlib plotting). This option can be overriden by
parameters in specific functions.
"""
if self._local.sampling_options is None:
self._local.sampling_options = sampling_options.SamplingOptions()

return self._local.sampling_options

@property
def compute(self) -> compute_options.ComputeOptions:
"""Thread-local options controlling object computation."""
if self._local.compute_options is None:
self._local.compute_options = compute_options.ComputeOptions()

return self._local.compute_options

@property
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/_config/test_threaded_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import threading

import bigframes._config


def test_mutate_options_threaded():
options = bigframes._config.Options()
options.display.max_rows = 50
result_dict = {"this_before": options.display.max_rows}

def mutate_options_threaded(options, result_dict):
result_dict["other_before"] = options.display.max_rows

options.display.max_rows = 100
result_dict["other_after"] = options.display.max_rows

thread = threading.Thread(
target=(lambda: mutate_options_threaded(options, result_dict))
)
thread.start()
thread.join(1)
result_dict["this_after"] = options.display.max_rows

assert result_dict["this_before"] == 50
assert result_dict["this_after"] == 50
assert result_dict["other_before"] == 25
assert result_dict["other_after"] == 100