Skip to content

Instantly share code, notes, and snippets.

@NightBlues
Created December 14, 2016 15:19
Show Gist options
  • Save NightBlues/ad0ca520922b52fc459cff282cec7eec to your computer and use it in GitHub Desktop.
Save NightBlues/ad0ca520922b52fc459cff282cec7eec to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
import threading
dyn = threading.local()
@contextmanager
def dlet(**new):
old = {}
for name, value in new.items():
old[name] = getattr(dyn, name, None)
setattr(dyn, name, value)
yield
for name, value in old.items():
setattr(dyn, name, value)
dyn.is_async = False
print "we call sync function, ", dyn.is_async
with dlet(is_async=True):
print "we call async function, ", dyn.is_async
with dlet(is_async=True):
print "we call async function, ", dyn.is_async
with dlet(is_async=False):
print "we call sync function, ", dyn.is_async
print "we call async function again, ", dyn.is_async
print "we call async function again, ", dyn.is_async
print "we call sync function again, ", dyn.is_async
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment