Skip to content

Thread safety issues with googleapiclient #54

@joshtemple

Description

@joshtemple

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions