You can use the __enter__ / __exit__ methods when running in SDK mode:
__enter__
__exit__
Also, you might consider adding the data to the Grasshopper document instead: