GithubHelp home page GithubHelp logo

Comments (7)

martindurant avatar martindurant commented on August 11, 2024 1

Your implementation looks good.

from s3fs.

pb222xd avatar pb222xd commented on August 11, 2024

🫡

from s3fs.

pb222xd avatar pb222xd commented on August 11, 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

from s3fs.

martindurant avatar martindurant commented on August 11, 2024

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.

from s3fs.

pb222xd avatar pb222xd commented on August 11, 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

from s3fs.

martindurant avatar martindurant commented on August 11, 2024

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.

from s3fs.

pb222xd avatar pb222xd commented on August 11, 2024

Thank you very much again🫡

from s3fs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.