python Future的两种使用
1、通过submit提交任务创建获得任务的future对象,通过as_completed等待future对象结束,获得结果。as_completed接收future对象的迭代器。
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: # Start the load operations and mark each future with its URL future_to_url = {executor.submit(load_url, url, 60): url for url in URLS} for future in concurrent.futures.as_completed(future_to_url): url = future_to_url[future] try: data = future.result() except Exception as exc: print('%r generated an exception: %s' % (url, exc)) else: print('%r page is %d bytes' % (url, len(data)))
2、通过map方法批量执行任务,取得结果。但是,map方法的返回结果的顺序与调用顺序传输的反复对象的顺序相同,前面的任务过长的话,后面的任务的返回会堵塞。
with concurrent.futures.ProcessPoolExecutor() as executor: for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)): print('%d is prime: %s' % (number, prime))
微信扫描下方的二维码阅读更多精彩内容
每日分享到群里,或者推荐给朋友会得大量积分,机会可以兑换微信零钱红包,具体请点击这里,得到了微信红包可以用来支持大飞哥
大飞哥能不能加鸡腿就看各位了!
开发者微信
开发者微信反馈BUG或者VIP可以添加,其他情况反馈可能不及时,见谅
版权声明
初衷是提供高清手机电脑壁纸等图片素材免费分享下载,禁止商用。图片素材来源网络,版权归原作者所有,若有侵权问题敬请告知我们!
【友情提醒】:
因平台原因不易展示大尺度写真,有的写真展示越少代表此套写真越性感,特别是xiuren等写真每一套写真完整套图50-100张不等。更多内容的欣赏请移步 点击这里
【更多图集移步】:
每日更新-点击这里
漂亮小姐姐-点击这里
性感美女-点击这里
清纯女孩-点击这里
xiuren专栏-点击这里
整站资源下载-点击这里