|
| 1 | +# Batch |
| 2 | + |
| 3 | +Each HTTP connection that your application makes results in a certain amount of overhead. |
| 4 | +This library supports batching, |
| 5 | +to allow your application to put several API calls into a single HTTP request. |
| 6 | +Examples of situations when you might want to use batching: |
| 7 | +* You have many small requests to make and would like to minimize HTTP request overhead. |
| 8 | +* A user made changes to data while your application was offline, |
| 9 | + so your application needs to synchronize its local data with the server |
| 10 | + by sending a lot of updates and deletes. |
| 11 | + |
| 12 | +**Note**: You're limited to 1000 calls in a single batch request. |
| 13 | +If you need to make more calls than that, use multiple batch requests. |
| 14 | + |
| 15 | +**Note**: You cannot use a |
| 16 | +[media upload](/api-client-library/python/guide/media_upload) |
| 17 | +object in a batch request. |
| 18 | + |
| 19 | +## Details |
| 20 | +You create batch requests by calling `new_batch_http_request()` on your service |
| 21 | +object, which returns a |
| 22 | +[BatchHttpRequest](https://google.github.io/google-api-python-client/docs/epy/googleapiclient.http.BatchHttpRequest-class.html) |
| 23 | +object, and then calling `add()` for each request you want to execute. |
| 24 | +You may pass in a callback with each request that is called with the response to that request. |
| 25 | +The callback function arguments are: |
| 26 | +a unique request identifier for each API call, |
| 27 | +a response object which contains the API call response, |
| 28 | +and an exception object which may be set to an exception raised by the API call. |
| 29 | +After you've added the requests, you call `execute()` to make the requests. |
| 30 | +The `execute()` function blocks until all callbacks have been called. |
| 31 | + |
| 32 | +In the following code snippet, |
| 33 | +two API requests are batched to a single HTTP request, |
| 34 | +and each API request is supplied a callback: |
| 35 | + <pre class="prettyprint"> |
| 36 | +See below</pre> |
| 37 | +You can also supply a single callback that gets called for each response: |
| 38 | + |
| 39 | + <pre class="prettyprint">See below</pre> |
| 40 | + |
| 41 | +The |
| 42 | +[add()](https://google.github.io/google-api-python-client/docs/epy/googleapiclient.http.BatchHttpRequest-class.html#add) |
| 43 | +method also allows you to supply a <code>request_id</code> parameter for each request. |
| 44 | +These IDs are provided to the callbacks. |
| 45 | +If you don't supply one, the library creates one for you. |
| 46 | +The IDs must be unique for each API request, |
| 47 | +otherwise `add()` raises an exception. |
| 48 | + |
| 49 | +If you supply a callback to both `new_batch_http_request()` and `add()`, they both get called. |
| 50 | + |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +```python |
| 55 | +def list_animals(request_id, response, exception): |
| 56 | + if exception is not None: |
| 57 | + # Do something with the exception |
| 58 | + pass |
| 59 | + else: |
| 60 | + # Do something with the response |
| 61 | + pass |
| 62 | + |
| 63 | +def list_farmers(request_id, response): |
| 64 | + """Do something with the farmers list response.""" |
| 65 | + pass |
| 66 | + |
| 67 | +service = build('farm', 'v2') |
| 68 | + |
| 69 | +batch = service.new_batch_http_request() |
| 70 | + |
| 71 | +batch.add(service.animals().list(), callback=list_animals) |
| 72 | +batch.add(service.farmers().list(), callback=list_farmers) |
| 73 | +batch.execute(http=http) |
| 74 | +``` |
| 75 | + |
| 76 | +```python |
| 77 | + |
| 78 | +def insert_animal(request_id, response, exception): |
| 79 | + if exception is not None: |
| 80 | + # Do something with the exception |
| 81 | + pass |
| 82 | + else: |
| 83 | + # Do something with the response |
| 84 | + pass |
| 85 | + |
| 86 | +service = build('farm', 'v2') |
| 87 | + |
| 88 | +batch = service.new_batch_http_request(callback=insert_animal) |
| 89 | + |
| 90 | +batch.add(service.animals().insert(name="sheep")) |
| 91 | +batch.add(service.animals().insert(name="pig")) |
| 92 | +batch.add(service.animals().insert(name="llama")) |
| 93 | +batch.execute(http=http) |
| 94 | +``` |
0 commit comments