2024-09-01
Python
00

https://www.cnblogs.com/haoabcd2010/p/10614509.html

python
import time from concurrent import futures from concurrent.futures import ThreadPoolExecutor def display(args): print(time.strftime('[%H:%M:%S]', time.localtime()), end=' ') print(args) def task(n): """只是休眠""" display('begin sleep {}s.'.format(n)) time.sleep(n) display('ended sleep {}s.'.format(n)) def do_many_task_inorder(): """多线程 按任务发布顺序依次等待完成 """ tasks = [5, 4, 3, 2, 1] with ThreadPoolExecutor(max_workers=3) as executor: future_list = [executor.submit(task, arg) for arg in tasks] display('非阻塞运行') for future in future_list: display(future) display('统一结束(有序)') for future in future_list: display(future.result()) def do_many_task_disorder(): """多线程执行 先完成先显示 """ tasks = [5, 4, 3, 2, 1] with ThreadPoolExecutor(max_workers=3) as executor: future_list = [executor.submit(task, arg) for arg in tasks] display('非阻塞运行') for future in future_list: display(future) display('统一结束(无序)') done_iter = futures.as_completed(future_list) # generator for done in done_iter: display(done) if __name__ == '__main__': do_many_task_inorder() do_many_task_disorder()
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:Dong

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!