实现最大下载数量5个线程、最大上传数量5个线程;等到全部下载且上传完成后结束任务
import time
import random
from concurrent.futures import ThreadPoolExecutor
THREAD_POOL_DOWNLOAD_SAMPLE = 3
pool_download = ThreadPoolExecutor(max_workers=THREAD_POOL_DOWNLOAD_SAMPLE)
pool_upload = ThreadPoolExecutor(max_workers=THREAD_POOL_DOWNLOAD_SAMPLE)
pool_download_manager = {}
pool_upload_manager = {}
is_already_printed_wait_info = False
is_already_add_in_download_pool = {}
def http_download(thread_id):
s = random.randint(2, 4)
time.sleep(s)
print "下载", thread_id, "睡眠了", s
return s
def http_upload(thread_id):
s = random.randint(2, 4)
time.sleep(s)
print "上传", thread_id, "睡眠了", s
return s
thread_id = 0
add_num = 5
while 1:
if thread_id > 20:
add_num = 0
# 2、添加下载任务、修改样本状态
for i in range(add_num):
thread_id += 1
if pool_download_manager.get(thread_id, None) is not None:
print 'sample_name continue', thread_id
continue
is_already_add_in_download_pool[thread_id] = 1
pool_download_manager[thread_id] = pool_download.submit(http_download, thread_id)
print "下载任务存量", len(pool_download_manager), "上传任务存量", len(pool_upload_manager)
time.sleep(1)
# 3、检查下载的任务有无完成
for thread_id in pool_download_manager.keys():
# 下载的线程好了吗?
is_done = pool_download_manager[thread_id].done()
if is_done:
res = pool_download_manager.get(thread_id).result()
del pool_download_manager[thread_id]
# ----- 开始上传 -----
pool_upload_manager[thread_id] = pool_upload.submit(http_upload, thread_id)
# 4、检查上传的任务有无完成
for thread_id in pool_upload_manager.keys():
# 上传的线程好了吗?
is_done = pool_upload_manager[thread_id].done()
if is_done:
res = pool_upload_manager.get(thread_id).result()
del pool_upload_manager[thread_id]
print "下载任务存量", len(pool_download_manager), "上传任务存量", len(pool_upload_manager)
if len(pool_download_manager) == 0:
break
if __name__ == '__main__':
pass
附:
┗━┃┣━
ThreadPoolExecutor:
┣━'map':按照添加线程的顺序去执行线程;方法为保证任务的顺序性即使第二个先结束,也要阻塞等一个好了,且必须用 fs = [Map.submit(fn, *args) for args in itertools.izip(*iterables)]的形式
┣━'shutdown':
┗━'submit':不按顺序执行线程,其他同'map';可使用wait(all_task,return_when=ALL_COMPLETED)等全部线程结束