赞
踩
提交任务的两种方式:
import random import time from concurrent.futures import ThreadPoolExecutor def la(name): print('%s is laing' % 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 拉了 《%s》kg' % (name, size)) if __name__ == '__main__': pool = ThreadPoolExecutor(13) shit1 = pool.submit(la, 'allen').result() weigh(shit1) shit2 = pool.submit(la, 'winnie').result() weigh(shit2) shit3 = pool.submit(la, 'vivian').result() weigh(shit3)
import random import time from concurrent.futures import ThreadPoolExecutor def la(name): print('%s is laing' % 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 拉了 《%s》kg' % (name, size)) if __name__ == '__main__': pool = ThreadPoolExecutor(13) pool.submit(la, 'allen').add_done_callback(weigh) pool.submit(la, 'winnie').add_done_callback(weigh) pool.submit(la, 'vivian').add_done_callback(weigh)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。