| 1 |
import asyncio |
| 2 |
|
| 3 |
from .context import Context as _Context |
| 4 |
|
| 5 |
|
| 6 |
class AsyncContext(_Context): |
| 7 |
""" |
| 8 |
Async Context for storing segments. |
| 9 |
|
| 10 |
Inherits nearly everything from the main Context class. |
| 11 |
Replaces threading.local with a task based local storage class, |
| 12 |
Also overrides clear_trace_entities |
| 13 |
""" |
| 14 |
def __init__(self, *args, loop=None, use_task_factory=True, **kwargs): |
| 15 |
super(AsyncContext, self).__init__(*args, **kwargs) |
| 16 |
|
| 17 |
self._loop = loop |
| 18 |
if loop is None: |
| 19 |
self._loop = asyncio.get_event_loop() |
| 20 |
|
| 21 |
if use_task_factory: |
| 22 |
self._loop.set_task_factory(task_factory) |
| 23 |
|
| 24 |
self._local = TaskLocalStorage(loop=loop) |
| 25 |
|
| 26 |
def clear_trace_entities(self): |
| 27 |
""" |
| 28 |
Clear all trace_entities stored in the task local context. |
| 29 |
""" |
| 30 |
if self._local is not None: |
| 31 |
self._local.clear() |
| 32 |
|
| 33 |
|
| 34 |
class TaskLocalStorage(object): |
| 35 |
""" |
| 36 |
Simple task local storage |
| 37 |
""" |
| 38 |
def __init__(self, loop=None): |
| 39 |
if loop is None: |
| 40 |
loop = asyncio.get_event_loop() |
| 41 |
self._loop = loop |
| 42 |
|
| 43 |
def __setattr__(self, name, value): |
| 44 |
if name in ('_loop',): |
| 45 |
# Set normal attributes |
| 46 |
object.__setattr__(self, name, value) |
| 47 |
|
| 48 |
else: |
| 49 |
# Set task local attributes |
| 50 |
task = asyncio.Task.current_task(loop=self._loop) |
| 51 |
if task is None: |
| 52 |
return None |
| 53 |
|
| 54 |
if not hasattr(task, 'context'): |
| 55 |
task.context = {} |
| 56 |
|
| 57 |
task.context[name] = value |
| 58 |
|
| 59 |
def __getattribute__(self, item): |
| 60 |
if item in ('_loop', 'clear'): |
| 61 |
# Return references to local objects |
| 62 |
return object.__getattribute__(self, item) |
| 63 |
|
| 64 |
task = asyncio.Task.current_task(loop=self._loop) |
| 65 |
if task is None: |
| 66 |
return None |
| 67 |
|
| 68 |
if hasattr(task, 'context') and item in task.context: |
| 69 |
return task.context[item] |
| 70 |
|
| 71 |
raise AttributeError('Task context does not have attribute {0}'.format(item)) |
| 72 |
|
| 73 |
def clear(self): |
| 74 |
# If were in a task, clear the context dictionary |
| 75 |
task = asyncio.Task.current_task(loop=self._loop) |
| 76 |
if task is not None and hasattr(task, 'context'): |
| 77 |
task.context.clear() |
| 78 |
|
| 79 |
|
| 80 |
def task_factory(loop, coro): |
| 81 |
""" |
| 82 |
Task factory function |
| 83 |
|
| 84 |
Fuction closely mirrors the logic inside of |
| 85 |
asyncio.BaseEventLoop.create_task. Then if there is a current |
| 86 |
task and the current task has a context then share that context |
| 87 |
with the new task |
| 88 |
""" |
| 89 |
task = asyncio.Task(coro, loop=loop) |
| 90 |
if task._source_traceback: # flake8: noqa |
| 91 |
del task._source_traceback[-1] # flake8: noqa |
| 92 |
|
| 93 |
# Share context with new task if possible |
| 94 |
current_task = asyncio.Task.current_task(loop=loop) |
| 95 |
if current_task is not None and hasattr(current_task, 'context'): |
| 96 |
setattr(task, 'context', current_task.context) |
| 97 |
|
| 98 |
return task |