Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 1 | from apiclient.discovery import build |
| 2 | from apiclient.discovery import HttpError |
| 3 | |
| 4 | import Queue |
| 5 | import httplib2 |
| 6 | import pickle |
| 7 | import threading |
| 8 | import time |
| 9 | |
| 10 | # Uncomment to get detailed logging |
| 11 | # httplib2.debuglevel = 4 |
| 12 | |
| 13 | |
| 14 | NUM_THREADS = 4 |
| 15 | NUM_ITEMS = 40 |
| 16 | |
| 17 | queue = Queue.Queue() |
| 18 | |
| 19 | |
| 20 | class Backoff: |
| 21 | """Exponential Backoff |
| 22 | |
| 23 | Implements an exponential backoff algorithm. |
| 24 | """ |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame^] | 25 | |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 26 | def __init__(self, maxretries=8): |
| 27 | self.retry = 0 |
| 28 | self.maxretries = maxretries |
| 29 | self.first = True |
| 30 | |
| 31 | def loop(self): |
| 32 | if self.first: |
| 33 | self.first = False |
| 34 | return True |
| 35 | else: |
| 36 | return self.retry < self.maxretries |
| 37 | |
| 38 | def fail(self): |
| 39 | self.retry += 1 |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame^] | 40 | delay = 2 ** self.retry |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 41 | time.sleep(delay) |
| 42 | |
| 43 | |
| 44 | def start_threads(credentials): |
| 45 | # Start up NUM_THREADS to handle requests |
| 46 | |
| 47 | def process_requests(): |
| 48 | http = httplib2.Http() |
| 49 | http = credentials.authorize(http) |
| 50 | |
| 51 | while True: |
| 52 | request = queue.get() |
| 53 | backoff = Backoff() |
| 54 | while backoff.loop(): |
| 55 | try: |
| 56 | request.execute(http) |
| 57 | break |
| 58 | except HttpError, e: |
| 59 | if e.resp.status in [402, 403, 408, 503, 504]: |
| 60 | print "Increasing backoff, got status code: %d" % e.resp.status |
| 61 | backoff.fail() |
| 62 | |
| 63 | print "Completed request" |
| 64 | queue.task_done() |
| 65 | |
| 66 | for i in range(NUM_THREADS): |
| 67 | t = threading.Thread(target=process_requests) |
| 68 | t.daemon = True |
| 69 | t.start() |
| 70 | |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 71 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame^] | 72 | def main(): |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 73 | f = open("moderator.dat", "r") |
| 74 | credentials = pickle.loads(f.read()) |
| 75 | f.close() |
| 76 | |
| 77 | start_threads(credentials) |
| 78 | |
| 79 | http = httplib2.Http() |
| 80 | http = credentials.authorize(http) |
| 81 | |
| 82 | p = build("moderator", "v1", http=http) |
| 83 | |
| 84 | series_body = { |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 85 | "data": { |
| 86 | "description": "An example of bulk creating topics", |
| 87 | "name": "Using threading and queues", |
| 88 | "videoSubmissionAllowed": False |
| 89 | } |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 90 | } |
| 91 | series = p.series().insert(body=series_body).execute() |
| 92 | print "Created a new series" |
| 93 | |
| 94 | for i in range(NUM_ITEMS): |
| 95 | topic_body = { |
| 96 | "data": { |
| 97 | "description": "Sample Topic # %d" % i, |
| 98 | "name": "Sample", |
| 99 | "presenter": "me" |
| 100 | } |
| 101 | } |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame^] | 102 | topic_request = p.topics().insert(seriesId=series['id']['seriesId'], |
| 103 | body=topic_body) |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 104 | print "Adding request to queue" |
| 105 | queue.put(topic_request) |
| 106 | |
| 107 | queue.join() |
| 108 | |
| 109 | |
| 110 | if __name__ == "__main__": |
| 111 | main() |