← Back to Pull requests

Added async support #656

Closed
unknown closed this pull request without merging on Mar 04, 2026 22:37 UTC
View on GitHub
unknown unknown commented on Jan 27, 2026 21:05 UTC
Author

Summary

This PR implements comprehensive asynchronous support for django-constance, making configuration access seamless and efficient in both sync and async Django environments.

Why?

Previously, using Constance in an async context (like a Django async def view) was problematic:

1. Thread Blocking: Accessing config.KEY synchronously inside an async view blocks the event loop, negating the performance benefits of async.
2. Database Errors: For the Database backend, Django's safety guards would raise a SynchronousOnlyOperation error because database queries are prohibited on the main async thread.
3. Manual Overhead: Developers had to manually wrap every Constance access in await sync_to_async(lambda: config.KEY)(), which is verbose and prone to errors.

This implementation removes these barriers by making the config object "async-agnostic"—it detects the execution context and provides the correct, non-blocking path automatically.

How to Use

1. Simple Async Access

In an async view or function, you can now simply await any configuration key:

`` python
async def my_view(request):
# Non-blocking async retrieval
if await config.MY_SETTING:
...


2. Async Updates and Bulk Retrieval

New native async methods have been added for updates and batch operations:

python

Update a setting asynchronously


await config.aset('MY_SETTING', 'new_value')

Fetch multiple settings in one non-blocking call


values = await config.amget(['SETTING_A', 'SETTING_B'])
`

3. Gradual Migration & Safety Guards

If you forget to use await inside an async loop, the app will still work (to prevent crashes), but it will:

  • Emit a Warning: A RuntimeWarning will be triggered: "Synchronous access to Constance setting 'KEY' inside an async loop. Use 'await config.KEY' instead."

  • Database Safety: If using the Database backend, it will still raise SynchronousOnlyOperation if accessed synchronously without await, nothing changed here.

  • Any previous implementations that use await sync_to_async(lambda: config.KEY)()` will continue to work without any issues or drawbacks. Dropping the sync_to_async-lambda combination will increase performance or at the very least developer experience though.
  • Key Technical Changes

  • AsyncValueProxy: A lazy-loading proxy that implements __await__ while supporting standard Python operators (==, +, bool(), etc.) for backward compatibility.

  • Native Backend Support:

  • - DatabaseBackend now uses afirst() and afetch() for non-blocking I/O.
    - RedisBackend leverages redis.asyncio for native speed.
  • Context Detection: Uses asyncio.get_running_loop() to switch behavior at runtime without performance penalty in synchronous code.
  • Verification

  • Added tests/test_async.py covering all new async paths.

  • All existing synchronous tests pass.
  • Diff: 360 lines (+270 / -90) across 4 files Reviewer: Mogost Review duration: 51932m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 360 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.352
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #656"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.6% (MEDIUM) · Higher = larger blast radius
    42.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.8% · Safe to merge
    16.8%