Skip to content

Instantly share code, notes, and snippets.

@dongguosheng
Last active December 19, 2015 17:19
Show Gist options
  • Save dongguosheng/5990636 to your computer and use it in GitHub Desktop.
Save dongguosheng/5990636 to your computer and use it in GitHub Desktop.
multi-threading with Queue in Python
import threading
import urllib
import Queue
import time
class Mythread(threading.Thread):
'''Download pictures.'''
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
self.daemon = True
self.start()
def run(self):
while(True):
url = self.queue.get()
urllib.urlretrieve(url, r'./' + url[32 : 51] + '.jpg')
self.queue.task_done()
urllist = ['http://soso7.gtimg.cn/sosopic/0/6704014884959496047/640',
'http://soso1.gtimg.cn/sosopic/0/5484709051946749088/640']
queue = Queue.Queue()
starttime = time.time()
def main():
# initialize a daemon thread pool for downloading pictures
print 'init daemon thread pool'
for i in range(2):
Mythread(queue)
# feed the queue with urls
for url in urllist:
queue.put(url)
# wait for the queue
queue.join()
print 'Time : %s' % (time.time() - starttime)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment