Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature Request: allow clients to act as context manager and add close() method to cleanup resources #575

Closed
@tswast

Description

@tswast

Since a transport object may be passed into a client constructor, the client does not close that transport in when the client object is deleted. This results in leaking socket resources in programs that create client objects dynamically.

Proposed design:

Add the following two ways to explicitly clean-up sockets owned by the client: (1, preferred) allow clients to act as a context manager and (2) a close() method.

  1. Context manager

    Allow all client objects to be used as a context manager. Call close() on the client in the exit method to clean up sockets owned by the client.

    In code samples with short-lived clients, this is the cleanup method we should recommend.

Example:

from google.cloud import bigquery_datatransfer_v1

with bigquery_datatransfer_v1.DataTransferServiceClient() as client:
    print(client.get_transfer_run('some run id').state)
  1. close() method

    Add a close() method to all client objects. When called, clean up sockets owned by the client, including the transport channel.

    Example:

from google.cloud import bigquery_datatransfer_v1

client = bigquery_datatransfer_v1.DataTransferServiceClient()
print(client.get_transfer_run('some run id').state)
client.close()  # clean up any open sockets

References:

Metadata

Metadata

Assignees

No one assigned

    Labels

    generatorBugs, features, and so forth pertaining to the generated client surfacepriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions