在我的python代碼中,我調用外部api來獲取圖像的url列表。對於這些網址中的每一個,我創建一個線程來生成縮略圖。這裏是感興趣的部分代碼:Python線程隊列和無響應線程
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]))
我的問題是,一些網址不工作,我的意思是,如果我複製粘貼在瀏覽器的URL它不斷載荷和載荷並加載多一點,直到返回超時。很明顯,我添加了多線程來加快速度。導致此問題的第一個URL是4,所以如果我加入...
# 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]
一切正常,並很快,否則它就會被阻塞很長一段時間,它終於終止執行。我想爲線程運行該函數設置某種超時(比如說1秒),如果url不起作用,並且線程在超時之前沒有完成,那麼退出該線程。
非常感謝您的幫助。
看起來像你應該有超時作爲URL請求的一部分,而不是讓線程負責殺死自己,如果它需要太長時間。有沒有辦法在你使用的這個庫中執行超時? – turbulencetoo
我還記得python中的多線程通常不會加快速度,因爲任何時候只有一個線程可以由解釋器執行。請參閱https://wiki.python.org/moin/GlobalInterpreterLock – turbulencetoo
@turbulencetoo對於讓url請求超時有一點意見。我更熟悉'多處理'與'線程',但不會「連接」採取可選的超時參數?另外,如果您有多個CPU,使用多處理功能將使您能夠真正並行運行。 – RobertB