Created
December 14, 2016 15:19
-
-
Save NightBlues/ad0ca520922b52fc459cff282cec7eec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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