Giter Club home page Giter Club logo

Comments (17)

kiddten avatar kiddten commented on June 13, 2024 1

Good idea! Thanks 👍

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

Sorry, I don't quite understand what you're saying. Can you give me an example?

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

There is an example:

class Manager:
    client_creds = ..
    user_creds = ..

    async def method1(self):
        # api call here
        async with Aiogoogle(self.client_creds, self.user_creds):
            ...

    async def method2(self):
        # api call here too
        async with Aiogoogle(self.client_creds, self.user_creds):
            ...

There will be overhead since I can't reuse Aiogoogle() resources across class methods..

Like with aiohttp.ClientSession for example

class Manager:
    session = ClientSession(..)

    async def function():
        await self.session.post()

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

Oh so you just want to minimize the amount of async ctx managers in your code, am I right?

In the case of aiohttp, when you call await session.post without an async ctx manager, it automatically opens an async ctx manager for you. I just thought it would be nice, if users where aware of when their session is open and when it's closed. It's possible to achieve what you're proposing with aiogoogle's auth managers though.

Building on your first example, you can instead do the following:

class Manager:
    client_creds = ..
    user_creds = ..
	aiogoogle = Aiogoogle(client_creds=client_creds, user_creds=user_creds)

    async def method1(self):
        # api call here
        async with self.aiogoogle as aiogoogle:
			await aiogoogle.as_user(
				...
			)
            ...

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

Another point there is that aiohttp for example executes several requests (post, get, etc) within same session. And I am able to close that explicitly when needed. Aiogoogle() ctx manager re-instantiates actual session (used to perform underlying requests) every time..

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

https://github.com/omarryhan/aiogoogle/blob/master/aiogoogle/client.py#L223
If we add here smth like that:

        # Send authorized requests
        if not self.active_session:
            self.active_session = self.session_factory()
        return await self.active_session.send(*authorized_requests, timeout=timeout, full_res=full_res, session_factory=self.session_factory)

we will be manage session explicitly.. Sure we should close() that session manually in future

Thought I could manage that directly via .active_session attribute

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

Gotcha!

You're right, it's a bit inefficient to instantiate a new ClientSession with every call. I just released version 0.1.7. Can you check if that solves your problem?

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

Thank you for quick fix! But I'm stuck with next problem:

class GooglePhotosManager:
    def __init__(self):
        self.client_cred = conf.google_photos.client.as_dict()
        self.user_cred = conf.google_photos.user.as_dict()
        self.client = Aiogoogle(client_creds=self.client_cred, user_creds=self.user_cred)
        self.client.active_session = self.client.session_factory()

    async def main(self):
        print(self.client.active_session)
        await self.client.__aenter__()
        photos = await self.client.discover('photoslibrary', 'v1')

fails on last line with RuntimeError: Task <Task pending coro=<AiohttpSession.send.<locals>.get_content() running at /.venv/lib/python3.7/site-packages/aiogoogle/sessions/aiohttp_session.py:135> cb=[gather.<locals>._done_callback() at /usr/lib/python3.7/asyncio/tasks.py:664]> got Future <Future pending> attached to a different loop

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

What's the reason behind:

  1. self.client.active_session = self.client.session_factory()
  2. await self.client.__aenter__() ?

Just write async with normally. After the previous update, aiogoogle doesn't instantiate a new session with each request it uses an old one if there's any.

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

But with several ctx managers there will be several sessions. In case of managing self.client.active_session manually I could use same session in different class methods -- (without ctx managers actually).

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

Why don't you do this for example:

class GooglePhotosMan:
	async def main_task(self):
		async with self.client:
			await first_method()
			await second_method()

?

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

@omarryhan there is an issue still..

After first usage Aiogoogle as ctx manager there will be closed session bound to Aiogoogle instance..

aclient =Aiogoogle(client_creds=..., user_creds=...,)
async with aclient as client:
    photos = await client.discover('photoslibrary', 'v1')
async with aclient as client:
     # there will be closed session instance

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

Actually, I just ran into this issue myself in production... I ended up disposing of each client after every session close.

At this point, I don't think it's an issue with Aiogoogle.
Checkout this issue: aio-libs/aiohttp#787
And this comment: aio-libs/aiohttp#787 (comment). He's the lead maintainer of aiohttp, so I think he knows best.

One more solution (that I haven't tried) is to manually set keep-alive. Hope that helps.

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

But you are explicitly closing active session on ctx aexit..

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

Yes, that's because it's how aiohttp.ClientSession and other sessions implement it. I don't think it's correct to change this behaviour. How do you think I should implement instead. Can you make a pull request?

from aiogoogle.

kiddten avatar kiddten commented on June 13, 2024

That is okay.
In my case to reuse session I just avoid using ctx managers with aiogoogle client and manage session manually for my needs.
Example: https://github.com/kiddick/getcam/blob/master/shot/gphotos.py#L75

from aiogoogle.

omarryhan avatar omarryhan commented on June 13, 2024

Cool! I'm closing this issue now. Please reopen it if you face any related issues.

from aiogoogle.

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.