Added sample that shows how to use threads and queues to batch up requests.
diff --git a/samples/threadqueue/main.py b/samples/threadqueue/main.py
new file mode 100644
index 0000000..5e9314e
--- /dev/null
+++ b/samples/threadqueue/main.py
@@ -0,0 +1,107 @@
+from apiclient.discovery import build
+from apiclient.discovery import HttpError
+
+import Queue
+import httplib2
+import pickle
+import threading
+import time
+
+# Uncomment to get detailed logging
+# httplib2.debuglevel = 4
+
+
+NUM_THREADS = 4
+NUM_ITEMS = 40
+
+queue = Queue.Queue()
+
+
+class Backoff:
+ """Exponential Backoff
+
+ Implements an exponential backoff algorithm.
+ """
+ def __init__(self, maxretries=8):
+ self.retry = 0
+ self.maxretries = maxretries
+ self.first = True
+
+ def loop(self):
+ if self.first:
+ self.first = False
+ return True
+ else:
+ return self.retry < self.maxretries
+
+ def fail(self):
+ self.retry += 1
+ delay = 2**self.retry
+ time.sleep(delay)
+
+
+def start_threads(credentials):
+ # Start up NUM_THREADS to handle requests
+
+ def process_requests():
+ http = httplib2.Http()
+ http = credentials.authorize(http)
+
+ while True:
+ request = queue.get()
+ backoff = Backoff()
+ while backoff.loop():
+ try:
+ request.execute(http)
+ break
+ except HttpError, e:
+ if e.resp.status in [402, 403, 408, 503, 504]:
+ print "Increasing backoff, got status code: %d" % e.resp.status
+ backoff.fail()
+
+ print "Completed request"
+ queue.task_done()
+
+ for i in range(NUM_THREADS):
+ t = threading.Thread(target=process_requests)
+ t.daemon = True
+ t.start()
+
+def main():
+
+ f = open("moderator.dat", "r")
+ credentials = pickle.loads(f.read())
+ f.close()
+
+ start_threads(credentials)
+
+ http = httplib2.Http()
+ http = credentials.authorize(http)
+
+ p = build("moderator", "v1", http=http)
+
+ series_body = {
+ "description": "An example of bulk creating topics",
+ "name": "Using threading and queues",
+ "videoSubmissionAllowed": False
+ }
+ series = p.series().insert(body=series_body).execute()
+ print "Created a new series"
+
+ for i in range(NUM_ITEMS):
+ topic_body = {
+ "data": {
+ "description": "Sample Topic # %d" % i,
+ "name": "Sample",
+ "presenter": "me"
+ }
+ }
+ topic_request = p.topics().insert(seriesId=series['id']['seriesId'], body=topic_body)
+ print "Adding request to queue"
+ queue.put(topic_request)
+
+ queue.join()
+
+
+if __name__ == "__main__":
+ main()