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

Thread safety issues with googleapiclient #54

Open
joshtemple opened this issue Dec 4, 2023 · 0 comments
Open

Thread safety issues with googleapiclient #54

joshtemple opened this issue Dec 4, 2023 · 0 comments

Comments

@joshtemple
Copy link

Based on the docs here, a Google API service should not be shared across threads because of lack of thread safety in httplib2.

The google-api-python-client library is built on top of the httplib2 library, which is not thread-safe. Therefore, if you are running as a multi-threaded application, each thread that you are making requests from must have its own instance of httplib2.Http().

The easiest way to provide threads with their own httplib2.Http() instances is to either override the construction of it within the service object or to pass an instance via the http argument to method calls.

Problematic code

In api.py, a ProcurementApi is created at the module level.

procurement_api = ProcurementApi(settings.MARKETPLACE_PROJECT)

I believe this creates a single instance of the service, which would be shared across threads.

class ProcurementApi(object):
"""Utilities for interacting with the Procurement API."""
def __init__(self, project_id):
self.service = build(PROCUREMENT_API, "v1", cache_discovery=False)

For us, this has resulted in some head-scratching HTTP and SSL errors, of which I'm fairly certain this is the cause.

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

No branches or pull requests

1 participant