Skip to content

Commit

Permalink
Use task as state whenever possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
fantix committed Aug 29, 2018
1 parent 9b23258 commit 278ad10
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions contextvars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import collections.abc
import threading

Expand Down Expand Up @@ -186,15 +187,25 @@ def copy_context():


def _get_context():
ctx = getattr(_state, 'context', None)
state = _get_state()
ctx = getattr(state, 'context', None)
if ctx is None:
ctx = Context()
_state.context = ctx
state.context = ctx
return ctx


def _set_context(ctx):
_state.context = ctx
state = _get_state()
state.context = ctx


def _get_state():
loop = asyncio._get_running_loop()
if loop is None:
return _state
task = asyncio.Task.current_task(loop=loop)
return _state if task is None else task


_state = threading.local()

0 comments on commit 278ad10

Please sign in to comment.