blob: 50bfad05bf58d503a57aaebcbd0b10147d7c7052 [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
Grant Timmerman5c11b0a2019-06-26 07:39:48 -070012# Create a new Http() object for every request
13def build_request(http, *args, **kwargs):
14 new_http = httplib2.Http()
15 return apiclient.http.HttpRequest(new_http, *args, **kwargs)
16service = build('api_name', 'api_version', requestBuilder=build_request)
17
18# Pass in a new Http() manually for every request
19service = build('api_name', 'api_version')
20http = httplib2.Http()
21service.stamps().list().execute(http=http)
Bu Sun Kim8496ebe2019-08-12 18:04:35 -070022```