# 提交任务的俩方式 #1,同步调用:提交完任务,在原地等待任务执行完毕,拿到结果,再执行下一段代码,导致程序串行执行 import time, random from concurrent.futures import ThreadPoolExecutor def la(name): print('%s 在拉屎!'%name) time.sleep(random.randint(3,5)) res = random.randint(7, 13) * '#' return {'name':name, 'res': res} def weigh(shit): name = shit['name'] size = len(shit['res']) print('%s拉了%skg屎'%(name, size)) if __name__ == '__main__': pool = ThreadPoolExecutor(13) shit1 = pool.submit(la, 'alex').result() print(shit1) weigh(shit1) shit2 = pool.submit(la, 'wupeqi').result() weigh(shit2) shit3 = pool.submit(la, 'yuanhao').result() weigh(shit3) #2,异步调用 import time, random from concurrent.futures import ThreadPoolExecutor def la(name): print('%s 在拉屎!'%name) time.sleep(random.randint(3,5)) res = random.randint(7, 13) * '#' return {'name':name, 'res': res} def weigh(shit): shit = shit.result() name = shit['name'] size = len(shit['res']) print('%s拉了%skg屎'%(name, size)) if __name__ == '__main__': pool = ThreadPoolExecutor(13) shit1 = pool.submit(la, 'alex').add_done_callback(weigh) shit2 = pool.submit(la, 'wupeqi').add_done_callback(weigh) shit3 = pool.submit(la, 'yuanhao').add_done_callback(weigh) #add_done_callback(函数),称为回调机制