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

When using http.close()? #178

Closed
yohplala opened this issue Sep 24, 2020 · 2 comments
Closed

When using http.close()? #178

yohplala opened this issue Sep 24, 2020 · 2 comments
Labels

Comments

@yohplala
Copy link

yohplala commented Sep 24, 2020

Hi,

I am using httplib2 in python function through Google API python client.
Recently was added to Google API python client a close()method relying on http.close().

My google Drive connector is a class:

  • Google Drive service is created in init and becomes a property of my Google drive object. So I will certainly not use the close()method that was implemented in this lib as I need to re-use this serviceproperty afterwards.
  • to manage 'thread-safety', I create an http object for each connection in a dedicated function (as recommended in Google Drive documentation).

For instance to upload a file:

import httplib2

def write(self, folder_id: str, file_name: str, **kwargs):
    # Upload (caching the authorized `http` object used to run `next_chunk()`)
    media = googleapiclient.http.MediaFileUpload(file_name, resumable=True)
    file_metadata = {'name': file_name, 'parents': [folder_id]}
    request = self.drive.files().create(body=file_metadata, media_body=media, fields='id')
    # Set timeout to 10 minutes for upload of big chunks (is used when creating the `http` object)
    socket.setdefaulttimeout(600)
    auth_http = googleapiclient._auth.authorized_http(self.creds)        # here, creation of my local http object
    auth_http.cache = httplib2.FileCache(self.cache_path)
    response = None
    while response is None:
        status, response = request.next_chunk(num_retries=4, http=auth_http)
                                                                         # shoud I add here a call to close this http object?

Ok, this all seems very Google API python client related, sorry for the long introduction. :)

Now the httplib2 related question.
After the 10mn timeout I have setup in this write()function, will the httpobject naturally close, or do I have to call the http.close()method to ensure it is closed?

I thank you for your guidance.
Have a good day,
Bests,

@yohplala yohplala changed the title When using httep.close()? When using http.close()? Sep 24, 2020
@temoto
Copy link
Member

temoto commented Sep 24, 2020

In general you can call Http.close() when you know you will not be doing more requests to same hosts for a while. It only applies to long lived Http objects. If your auth_http goes out of scope and deleted by reference counter, it should close all connections without explicit .close() call.

Please open separate bug report if you observe background connections not closed after destructing Http object. That would be a bug.

@yohplala
Copy link
Author

Thanks @temoto , crystal clear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants