In my python code I make a call to an external api to get a list of images' urls. For each of these urls I create a thread to generate a thumbnail. Here is the part of the code of interest:
def process_image(image, size, cropping, counter, queue):
options = dict(crop=cropping)
img = get_thumbnail(image['url'], size, **options)
queue.put((counter, img))
return img
...
queue = Queue()
# Get some information about an artist. Images are also included.
artist = get_profile(artist_id, buckets)
# Generate images' thumbnails
threads = [Thread(target=process_image, args=(img, '500', 'center', counter, queue)) for counter, img in enumerate(artist.data['images'])]
for p in threads:
p.start()
for p in threads:
p.join()
imgs = []
# Collect processed images from threads
while not queue.empty():
el = queue.get()
imgs.append((el[0], el[1]))
My problem is that some of the urls don't work, what I mean is that if I copy-paste the url in the browser it keeps loading and loading and loading a bit more until a Time Out is returned. Obviously I added multithreading to speed things up. The first URL that causes this problem is the 4th one, so if I add...
# Generate images' thumbnails
threads = [Thread(target=process_image, args=(img, '500', 'center', counter, queue)) for counter, img in enumerate(artist.data['images'])]
treads = threads[:3]
everything works as expected and very quick, otherwise it gets blocked for a long time and it finally terminates the execution. I would like to set some kind of timeout (say 1 second) for the thread to run the function and if the url does not work and the thread does not finish before the timeout then exit that thread.
Thank you for your help in advance.