-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy paththumbnail_url_controller.py
291 lines (229 loc) · 8.49 KB
/
thumbnail_url_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# Code Listing #5
"""
Thumbnail producer/consumer - Limiting rate of production of data (URLs) using a condition object.
"""
import threading
import time
import string
import random
import uuid
import urllib.request
from PIL import Image
from queue import Queue
class ThumbnailURLController(threading.Thread):
""" A rate limiting controller thread for URLs using conditions """
def __init__(self, rate_limit=0, nthreads=0):
# Configured rate limit
self.rate_limit = rate_limit
# Number of producer threads
self.nthreads = nthreads
self.count = 0
self.start_t = time.time()
self.flag = True
self.cond = threading.Condition()
threading.Thread.__init__(self)
def increment(self):
# Increment count of URLs
self.count += 1
def calc_rate(self):
rate = 60.0*self.count/(time.time() - self.start_t)
return rate
def run(self):
while self.flag:
rate = self.calc_rate()
if rate<self.rate_limit:
with self.cond:
print('Notifying all...')
self.cond.notify_all()
print('Controller quitting')
def stop(self):
self.flag = False
self.join()
def throttle(self, thread):
""" Throttle threads to manage rate """
# Current total rate
rate = self.calc_rate()
print('Current Rate',rate)
# If rate > limit, add more sleep time to thread
diff = abs(rate - self.rate_limit)
sleep_diff = diff/(self.nthreads*60.0)
if rate>self.rate_limit:
# Adjust threads sleep_time
thread.sleep_time += sleep_diff
# Hold this thread till rate settles down with a 5% error
with self.cond:
print('Controller, rate is high, sleep more by',rate,sleep_diff)
while self.calc_rate() > self.rate_limit:
self.cond.wait()
elif rate<self.rate_limit:
print('Controller, rate is low, sleep less by',rate,sleep_diff)
# Decrease sleep time
sleep_time = thread.sleep_time
sleep_time -= sleep_diff
# If this goes off < zero, make it zero
thread.sleep_time = max(0, sleep_time)
# Dont hold the thread
class ThumbnailURL_Generator(threading.Thread):
""" Worker class that generates image URLs and supports throttling via
an external controller """
def __init__(self, queue, controller=None, sleep_time=1):
self.sleep_time = sleep_time
self.queue = queue
# A flag for stopping
self.flag = True
# sizes
self._sizes = (240,320,360,480,600,720)
# URL scheme
self.url_template = 'https://dummyimage.com/%s/%s/%s.jpg'
# Rate controller
self.controller = controller
# Internal id
self._id = uuid.uuid4().hex
threading.Thread.__init__(self, name='Producer-'+ self._id)
def __str__(self):
return 'Producer-'+self._id
def get_size(self):
return '%dx%d' % (random.choice(self._sizes),
random.choice(self._sizes))
def get_color(self):
return ''.join(random.sample(string.hexdigits[:-6], 3))
def run(self):
""" Main thread function """
while self.flag:
# generate image URLs of random sizes and fg/bg colors
url = self.url_template % (self.get_size(),
self.get_color(),
self.get_color())
# Add to queue
print(self,'Put',url)
self.queue.put(url)
self.controller.increment()
# Throttle after putting a few images
if self.controller.count>5:
self.controller.throttle(self)
time.sleep(self.sleep_time)
def stop(self):
""" Stop the thread """
self.flag = False
class ThumbnailImageSaver(object):
""" Class which saves URLs to thumbnail images and keeps a counter """
def __init__(self, limit=10):
self.limit = limit
self.lock = threading.Lock()
self.counter = {}
def thumbnail_image(self, url, size=(64,64), format='.png'):
""" Save image thumbnails, given a URL """
im=Image.open(urllib.request.urlopen(url))
# filename is last two parts of URL minus extension + '.format'
pieces = url.split('/')
filename = ''.join((pieces[-2],'_',pieces[-1].split('.')[0],'_thumb',format))
im.thumbnail(size, Image.ANTIALIAS)
im.save(filename)
print('Saved',filename)
self.counter[filename] = 1
return True
def save(self, url):
""" Save a URL as thumbnail """
with self.lock:
if len(self.counter)>=self.limit:
return False
self.thumbnail_image(url)
print('Count=>',len(self.counter))
return True
class ThumbnailImageSemaSaver(object):
""" Class which keeps an exact counter of saved images
and restricts the total count using a semaphore """
def __init__(self, limit=10):
self.limit = limit
self.counter = threading.BoundedSemaphore(value=limit)
self.count = 0
# Start time
self.start = time.time()
# Image saving rate
self.rate = 0
def acquire(self):
# Acquire counter, if limit is exhausted, it
# returns False
return self.counter.acquire(blocking=False)
def release(self):
# Release counter, incrementing count
return self.counter.release()
def thumbnail_image(self, url, size=(64,64), format='.png'):
""" Save image thumbnails, given a URL """
im=Image.open(urllib.request.urlopen(url))
# filename is last two parts of URL minus extension + '.format'
pieces = url.split('/')
filename = ''.join((pieces[-2],'_',pieces[-1].split('.')[0],format))
try:
im.thumbnail(size, Image.ANTIALIAS)
im.save(filename)
print('Saved',filename)
self.count += 1
except Exception as e:
print('Error saving URL',url,e)
# Image can't be counted, increment semaphore
self.release()
return True
def save(self, url):
""" Save a URL as thumbnail """
if self.acquire():
self.thumbnail_image(url)
return True
else:
print('Semaphore limit reached, returning False')
return False
class ThumbnailURL_Consumer(threading.Thread):
""" Worker class that consumes URLs and generates thumbnails """
def __init__(self, queue, saver):
self.queue = queue
self.flag = True
self.saver = saver
self.count = 0
# Internal id
self._id = uuid.uuid4().hex
threading.Thread.__init__(self, name='Consumer-'+ self._id)
def __str__(self):
return 'Consumer-' + self._id
def run(self):
""" Main thread function """
while self.flag:
url = self.queue.get()
print(self,'Got',url)
self.count += 1
if not self.saver.save(url):
# Limit reached, break out
print(self, 'Set limit reached, quitting')
break
def stop(self):
""" Stop the thread """
self.flag = False
if __name__ == '__main__':
from queue import Queue
import glob,os
os.system('rm -f *.png')
q = Queue(maxsize=2000)
controller = ThumbnailURLController(rate_limit=25, nthreads=3)
saver = ThumbnailImageSemaSaver(limit=100)
controller.start()
producers, consumers = [], []
for i in range(3):
t = ThumbnailURL_Generator(q, controller)
producers.append(t)
t.start()
for i in range(5):
t = ThumbnailURL_Consumer(q, saver)
consumers.append(t)
t.start()
for t in consumers:
t.join()
print('Joined', t, flush=True)
# To make sure producers dont block on a full queue
while not q.empty():
item=q.get()
print('Stopping controller')
controller.stop()
print('Stopping producers...')
for t in producers:
t.stop()
print('Stopped',t, flush=True)
print('Total number of PNG images',len(glob.glob('*.png')))