Skip to content

Instantly share code, notes, and snippets.

@palazzem
Last active October 5, 2017 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palazzem/6cd522c8c9724919397b5f8f11fff0c6 to your computer and use it in GitHub Desktop.
Save palazzem/6cd522c8c9724919397b5f8f11fff0c6 to your computer and use it in GitHub Desktop.
pygotham_context_propagator.py
import asyncio
def set_current_context(ctx, loop=None):
# retrieves the current Task
loop = loop or asyncio.get_event_loop()
task = asyncio.Task.current_task(loop=loop)
# attach the Context to the task!
setattr(task, '__current_ctx', ctx)
def get_current_context(loop=None):
# retrieves the current Task
loop = loop or asyncio.get_event_loop()
task = asyncio.Task.current_task(loop=loop)
# get the Context
ctx = getattr(task, '__current_ctx', None)
return ctx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment