Search code examples
google-cloud-platformgoogle-cloud-storagegoogle-cloud-pubsub

How to close connection objects created for Gcp-Storage and pub-sub using Google Cloud Platform's python libraries


My application is creating pub-sub objects using:

google.cloud import pubsub_v1
publisher_client = pubsub_v1.PublisherClient     

and storage objects using:

from google.cloud import storage
client = storage.Client()

how can I quickly and gracefully close/release these connection objects so that can be used at scale?


Solution

  • The client objects are in memory objects if functions required for the interaction with google services, but they don't open network connection since they are created and keep it opened. So, having many objects created may not be a problem, since they are in memory allocations, like variables.

    I have made a test creating thousands of clients in loop and there was no problem at all.

    But, thay can be closed if you want. Basically just calling the following should delete them by:

    google.cloud import pubsub_v1
    from google.cloud import storage
    
    publisher_client = pubsub_v1.PublisherClient()     
    client = storage.Client()
    
    del publisher_client
    del client
    

    Its also recommended calling the following functions before del the objects:

    publisher_client.stop():

    Asynchronously sends all outstanding messages and prevents future calls to publish(). Method should be invoked prior to deleting this Client() object in order to ensure that no pending messages are lost.

    client.close():

    Documentation is not very clear about it, but seams to close any files being read to be uploaded.


    The another problem you have facing in production related to the GOOGLE_APPLICATION_CREDENTIALS env variable is because you have to set it on the production env.

    • Create a service account on GCP IAM console;
    • Add proper permissions (Cloud Store Editor, Pub/Sub Editor (?))
    • create a key on it (json) download and set env variable GOOGLE_APPLICATION_CREDENTIALS pointing to /path/of/key/key.json
    • more details in this documentation.