def main():
ts = time()
client_id = os.getenv('IMGUR_CLIENT_ID')
if not client_id:
raise Exception("Couldn't find IMGUR_CLIENT_ID environment variable!")
download_dir = setup_download_dir()
links = [l for l in get_links(client_id) if l.endswith('.jpg')]
for link in links:
download_link(download_dir, link)
print('Took {}s'.format(time() - ts))
if __name__ == "__main__":
t1 = time()
for i in getNums(3):
processNum(i)
print "cost time is: {:.2f}s".format(time() - t1)
结果:
?
1
2
3
4
<_MainThread(MainThread, started 4436)>: 0 → 1
<_MainThread(MainThread, started 4436)>: 1 → 2
<_MainThread(MainThread, started 4436)>: 2 → 3
cost time is: 3.00s
from queue import Queue
from threading import Thread
class DownloadWorker(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
# Get the work from the queue and expand the tuple
# 从队列中获取任务并扩展tuple
directory, link = self.queue.get()
download_link(directory, link)
self.queue.task_done()
def main():
ts = time()
client_id = os.getenv('IMGUR_CLIENT_ID')
if not client_id:
raise Exception("Couldn't find IMGUR_CLIENT_ID environment variable!")
download_dir = setup_download_dir()
links = [l for l in get_links(client_id) if l.endswith('.jpg')]
# Create a queue to communicate with the worker threads
queue = Queue()
# Create 8 worker threads
# 创建八个工作线程
for x in range(8):
worker = DownloadWorker(queue)
# Setting daemon to True will let the main thread exit even though the workers are blocking
# 将daemon设置为True将会使主线程退出,即使worker都阻塞了
worker.daemon = True
worker.start()
# Put the tasks into the queue as a tuple
# 将任务以tuple的形式放入队列中
for link in links:
logger.info('Queueing {}'.format(link))
queue.put((download_dir, link))
# Causes the main thread to wait for the queue to finish processing all the tasks
# 让主线程等待队列完成所有的任务
queue.join()
print('Took {}'.format(time() - ts))
# coding=utf-8
#测试utf-8编码
from Queue import Queue
from threading import Thread
from single import *
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class ProcessWorker(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
# Get the work from the queue
num = self.queue.get()
processNum(num)
self.queue.task_done()
def main():
ts = time()
nums = getNums(4)
# Create a queue to communicate with the worker threads
queue = Queue()
# Create 4 worker threads
# 创建四个工作线程
for x in range(4):
worker = ProcessWorker(queue)
# Setting daemon to True will let the main thread exit even though the workers are blocking
# 将daemon设置为True将会使主线程退出,即使worker都阻塞了
worker.daemon = True
worker.start()
# Put the tasks into the queue
for num in nums:
queue.put(num)
# Causes the main thread to wait for the queue to finish processing all the tasks
# 让主线程等待队列完成所有的任务
queue.join()
print("cost time is: {:.2f}s".format(time() - ts))
if __name__ == "__main__":
main()
结果:
?
1
2
3
4
5
<ProcessWorker(Thread-4, started daemon 3900)>: 3 → 4<ProcessWorker(Thread-1, started daemon 3436)>: 2 → 3<ProcessWorker(Thread-3, started daemon 4576)>: 1 → 2
<ProcessWorker(Thread-2, started daemon 396)>: 0 → 1
cost time is: 1.01s
def main():
client_id = os.getenv('IMGUR_CLIENT_ID')
if not client_id:
raise Exception("Couldn't find IMGUR_CLIENT_ID environment variable!")
download_dir = setup_download_dir()
links = [l for l in get_links(client_id) if l.endswith('.jpg')]
q = Queue(connection=Redis(host='localhost', port=6379))
for link in links:
q.enqueue(download_link, download_dir, link)