Skip to content
Open
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
15 changes: 10 additions & 5 deletions guardrails/validator_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

def should_run_sync():
process_count = os.environ.get("GUARDRAILS_PROCESS_COUNT")
# Short-circuit for 'GUARDRAILS_RUN_SYNC', and avoid duplicate lower() & lookups
run_sync = os.environ.get("GUARDRAILS_RUN_SYNC", "false")
run_sync_lower = run_sync.lower()
if process_count is not None:
warnings.warn(
"GUARDRAILS_PROCESS_COUNT is deprecated"
Expand All @@ -37,13 +40,15 @@ def should_run_sync():
DeprecationWarning,
)
process_count = int(process_count)
run_sync = os.environ.get("GUARDRAILS_RUN_SYNC", "false")
bool_values = ["true", "false"]
if run_sync.lower() not in bool_values:
# If process_count == 1, immediately return True for efficiency
if process_count == 1:
return True
# Avoid reconstructing list for each call
if run_sync_lower not in ("true", "false"):
warnings.warn(
f"GUARDRAILS_RUN_SYNC must be one of {bool_values}! Defaulting to 'false'."
"GUARDRAILS_RUN_SYNC must be one of ['true', 'false']! Defaulting to 'false'."
)
return process_count == 1 or run_sync.lower() == "true"
return run_sync_lower == "true"


def get_loop() -> asyncio.AbstractEventLoop:
Expand Down