Comments (17)
Good idea! Thanks 👍
from aiogoogle.
Sorry, I don't quite understand what you're saying. Can you give me an example?
from aiogoogle.
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.
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.
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.
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.
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.
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.
What's the reason behind:
self.client.active_session = self.client.session_factory()
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.
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.
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.
@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.
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.
But you are explicitly closing active session on ctx aexit..
from aiogoogle.
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.
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.
Cool! I'm closing this issue now. Please reopen it if you face any related issues.
from aiogoogle.
Related Issues (20)
- Ability to upload from AsyncIterable object HOT 6
- Can we provide a way to test the package end to end? HOT 1
- URL formatting issue HOT 3
- URL encoding issues with Calendar IDs HOT 1
- Aiogoogle doesn't support the beta Drive Labels API HOT 5
- Token is not getting refreshed after its expired after around 1 hour HOT 17
- Inconsistent response. HOT 5
- Unclosed client session HOT 4
- URL formatting breaks the people.connections.list method HOT 1
- Updating google drive file contents yields corrupted file due to multipart HOT 5
- Authentication using application default method HOT 1
- HttpError doesn't include JSON error response with the "pipe_to" option enabled HOT 1
- paginn issue HOT 2
- Requesting Google Drive list with multiple pages always results in user_creds of None HOT 3
- Issue with `ServiceAccountCreds` with `universe_domain` HOT 2
- FYI sending messages with Gmail doesn't work HOT 1
- UserCreds oauth token not refreshed after 1hour HOT 1
- `trio` support broken: unexpected keyword argument 'auth_manager' HOT 3
- Should attributes in `HTTPError` be type annotated? HOT 1
- Module import error : contextvars HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiogoogle.