Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confused about using s3fs asynchronously #843

Closed
pb222xd opened this issue Jan 24, 2024 · 7 comments
Closed

Confused about using s3fs asynchronously #843

pb222xd opened this issue Jan 24, 2024 · 7 comments

Comments

@pb222xd
Copy link

pb222xd commented Jan 24, 2024

After reading s3fs' documentation on asynchronous development https://s3fs.readthedocs.io/en/latest/#async, I tried a lot but only the following code seems to work

import asyncio
from s3fs import S3FileSystem


async def main():
    s3 = S3FileSystem(key="sA3HIztxVB31tvWV9uQv",
                      secret="up8OZN7yje6I4HK6seNkXfccdSzmQA86PudmkfpR",
                      client_kwargs={'endpoint_url': "http://127.0.0.1:9000",
                                     'region_name': "auto"})
    session = await s3.set_session()
    # noinspection PyProtectedMember
    await s3._put("test.txt", "test2.txt")  # pylint: disable=protected-access
    await session.close()


if __name__ == '__main__':
    asyncio.run(main())

Is there any potential problem with my implementation? Thank you very much.

@martindurant
Copy link
Member

Your implementation looks good.

@pb222xd
Copy link
Author

pb222xd commented Jan 24, 2024

🫡

@pb222xd pb222xd closed this as completed Jan 24, 2024
@pb222xd pb222xd reopened this Jan 24, 2024
@pb222xd
Copy link
Author

pb222xd commented Jan 24, 2024

Sorry to bother you, but there is another point that I don’t quite understand. What is the difference between the two?

class MyS3:
    def __init__(self):
        self.s3 = S3FileSystem(key="AMIECjhoqrJSQZw9zl3S",
                               secret="SNNcoLOzxG8fX9rTjXABaiBkMlDfxAiWkQGmTH5N",
                               client_kwargs={'endpoint_url': "http://127.0.0.1:9000",
                                              'region_name': "auto"})
        self.session = None

    async def __aenter__(self):
        self.session = await self.s3.set_session(refresh=True)
        return self.s3

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self.session.close()


class MyS3:
    def __init__(self):
        self.s3 = S3FileSystem(key="AMIECjhoqrJSQZw9zl3S",
                               secret="SNNcoLOzxG8fX9rTjXABaiBkMlDfxAiWkQGmTH5N",
                               client_kwargs={'endpoint_url': "http://127.0.0.1:9000",
                                              'region_name': "auto"})
        self.session = None

    async def __aenter__(self):
        self.session = await self.s3.set_session()
        return self.s3

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        pass

Thank you very much for answering my question

@martindurant
Copy link
Member

You mean omitting await self.session.close() ? This should ideally happen automatically for you when the instance gets cleaned up, but because that can happen in any thread and potentially at interpreter shutdown time, you may see (harmless) warnings about unclosed sockets and not run coroutines.

@pb222xd
Copy link
Author

pb222xd commented Jan 24, 2024

I noticed:

if self._s3 is not None and not refresh:

In other words, I can reuse the session like the second code without calling session.close( ) and let session be garbage collected?
Thank you again for your patient answer

@martindurant
Copy link
Member

Correct, if you don't want to manage instances yourself, you can reuse them. fsspec caches all instances unless explicitly avoided (such as skip_instance_cache=True), so if you use the same init arguments, you get exactly the same object back, and can expect the connection pool etc to still be there. It's up to you whether you want to use the context formalism for state hygene or not.

@pb222xd
Copy link
Author

pb222xd commented Jan 24, 2024

Thank you very much again🫡

@pb222xd pb222xd closed this as completed Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants