blob: 69f369e21ca098b6c05f878ccf15fde64850a810 [file] [log] [blame] [view]
Grant Timmerman5c11b0a2019-06-26 07:39:48 -07001# Thread Safety
2
3This page contains important information about the thread safety of this library.
4
5## The httplib2.Http() objects are not thread-safe
6
7The google-api-python-client library is built on top of the [httplib2](https://github.com/httplib2/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()`.
8
9The 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.
10
Bu Sun Kim8496ebe2019-08-12 18:04:35 -070011```python
Bu Sun Kim205ae592020-11-18 05:40:02 -070012import google.auth
13import googleapiclient
14import google_auth_httplib2
15import httplib2
16from googleapiclient import discovery
17
Grant Timmerman5c11b0a2019-06-26 07:39:48 -070018# Create a new Http() object for every request
19def build_request(http, *args, **kwargs):
Bu Sun Kim205ae592020-11-18 05:40:02 -070020 new_htpp = google_auth_httplib2.AuthorizedHttp(credentials, http=httplib2.Http())
21 return googleapiclient.http.HttpRequest(new_http, *args, **kwargs)
22service = discovery.build('api_name', 'api_version', requestBuilder=build_request)
Grant Timmerman5c11b0a2019-06-26 07:39:48 -070023
24# Pass in a new Http() manually for every request
Bu Sun Kim205ae592020-11-18 05:40:02 -070025service = discovery.build('api_name', 'api_version')
26http = google_auth_httplib2.AuthorizedHttp(credentials, http=httplib2.Http())
Grant Timmerman5c11b0a2019-06-26 07:39:48 -070027service.stamps().list().execute(http=http)
Bu Sun Kim205ae592020-11-18 05:40:02 -070028```