-
Notifications
You must be signed in to change notification settings - Fork 5
/
aiocontextvars.py
80 lines (53 loc) · 1.99 KB
/
aiocontextvars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Top-level package for aiocontextvars."""
__author__ = """Fantix King"""
__email__ = 'fantix.king@gmail.com'
__version__ = '0.2.2'
import sys
from contextvars import Context, ContextVar, Token, copy_context
if sys.version_info < (3, 7):
import asyncio
import contextvars
import types
from contextvars import _state
def _get_context():
state = _get_state()
ctx = getattr(state, 'context', None)
if ctx is None:
ctx = Context()
state.context = ctx
return ctx
def _set_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
contextvars._get_context = _get_context
contextvars._set_context = _set_context
def create_task(loop, coro):
task = loop._orig_create_task(coro)
if task._source_traceback:
del task._source_traceback[-1]
task.context = copy_context()
return task
def _patch_loop(loop):
if loop and not hasattr(loop, '_orig_create_task'):
loop._orig_create_task = loop.create_task
loop.create_task = types.MethodType(create_task, loop)
return loop
def get_event_loop():
return _patch_loop(_get_event_loop())
def set_event_loop(loop):
return _set_event_loop(_patch_loop(loop))
def new_event_loop():
return _patch_loop(_new_event_loop())
_get_event_loop = asyncio.get_event_loop
_set_event_loop = asyncio.set_event_loop
_new_event_loop = asyncio.new_event_loop
asyncio.get_event_loop = asyncio.events.get_event_loop = get_event_loop
asyncio.set_event_loop = asyncio.events.set_event_loop = set_event_loop
asyncio.new_event_loop = asyncio.events.new_event_loop = new_event_loop
__all__ = ('ContextVar', 'Context', 'Token', 'copy_context')