Skip to content

Commit

Permalink
Defined SyncToAsync.thread_sensitive_context type
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Feb 4, 2021
1 parent 9734d47 commit 7aa8d7e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions asgiref/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import threading
import weakref
from concurrent.futures import Future, ThreadPoolExecutor
from typing import TYPE_CHECKING, Dict
from typing import Dict

from .current_thread_executor import CurrentThreadExecutor
from .local import Local
Expand Down Expand Up @@ -311,10 +311,12 @@ class SyncToAsync:

# Maintain a contextvar for the current execution context. Optionally used
# for thread sensitive mode.
if not TYPE_CHECKING:
thread_sensitive_context = (
contextvars.ContextVar("thread_sensitive_context") if contextvars else None
if sys.version_info >= (3, 7):
thread_sensitive_context: "contextvars.ContextVar[str]" = (
contextvars.ContextVar("thread_sensitive_context")
)
else:
thread_sensitive_context: None = None

# Maintaining a weak reference to the context ensures that thread pools are
# erased once the context goes out of scope. This terminates the thread pool.
Expand Down

0 comments on commit 7aa8d7e

Please sign in to comment.