Do I need to close subfs or does it share the lifecycle of the parent?
Good Evening!
I am trying to understand the lifecycle of SubFs objects, but haven't been able to find doc and my attempts at reading the code have been somewhat unsuccessful.
**Question: ** Do I need to explicitly close SubFS objects or will they share the lifecycle of the parent?
Just as an example, I am not sure if this code is problematic:
def get_cats(root_fs: FS):
catsFs: SubFS = root_fs.opendir("cat_pictures")
cats: List[Text] = catsFS.listdir(".")
return cats
Do I need to updated it to be this:
def get_cats(root_fs: FS):
with root_fs.opendir("cat_pictures") as catsFS:
cats: List[Text] = cats.listdir(".")
return cats
SubFS instances follow the same general rule as other filesystems, in that they should be closed (ideally with the context manager). However, closing a SubFS doesn't actually do anything, so your first get_cats would work, but your second get_cats would be recommended.
Closing a SubFS won't close the parent. But if you close the parent, any attempt to use the SubFS will raise a errors.FilesystemClosed I believe.
Hope that clears it up!
Closing a SubFS won't close the parent. But if you close the parent, any attempt to use the SubFS will raise a errors.FilesystemClosed I believe.
Is this true for all FS types? If so, this should be documented.
Do you mean the FilesystemClosed exception? I would have thought that it would be obvious you can't use a filesystem that has been closed, but you're right that it should be documented.
From the code, it seems like SubFs will never close his parent FS, it will just set the _closed variable to True.
Except if you're explicitly using a ClosingSubFs (see here).
The FS types that are using this class, as far as I can see, are the AppFS when you give a delimiter and the FTPFS when you give a sub directory.
Other than these 2, all other types of FS seems to use the SubFs directly.
Therefore, if you close a SubFs, you can still use his parent FS.