Created
August 9, 2021 16:11
-
-
Save sandeepkumar-skb/0f6be07925430323d22994c320814164 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Context Manager using object | |
class ContextManager(): | |
def __init__(self, name): | |
self.name = name | |
def __enter__(self, ): | |
print('nvtx_marker on for: {}'.format(self.name)) | |
return self | |
def __exit__(self,exc_type, exc_value, exc_traceback): | |
print("nvtx_marker off for: {}".format(self.name)) | |
with ContextManager("path1") as manager: | |
with ContextManager("path2") as manager: | |
print("with statement block") | |
print("") | |
print("#########################################") | |
print("") | |
# context manager using decorator | |
@contextmanager | |
def ContextManager_alt(name): | |
print("nvtx marker on for {}".format(name)) | |
yield | |
print("nvtx marker off for {}".format(name)) | |
with ContextManager_alt("path1") as manager: | |
with ContextManager_alt("path2") as manager: | |
print("with statement block") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: