酷站(www.ku0.com)-致力于为互联网从业者提供动力!

热门关键词:  企业  as  baidu  c4rp3nt3r  美女
酷站

python

旗下栏目: vbs hta htc perl DOS/BAT python VBA ColdFusion autoit PowerShell seraphzone linux shell Lua Golang Erlang ruby 游戏 远程脚本

Python线程池用法的教程

来源:互联网搜集 作者:秩名 人气: 发布时间:2019-10-03
本篇文章主要介绍了Python线程池用法的教程,对大家的学习或者工作具有一定的参考学习价值,感兴趣的小伙伴们可以参考一下,也感谢大家对酷站(ku0.com)的支持。
代码如下:

# -*- coding:utf-8 -*-
#! python3
'''
Created on 2019-10-2
@author: Administrator
'''
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import os,time,random
def task(n):
  print('%s is runing' %os.getpid())
  time.sleep(random.randint(1,3))
  return n**2
if __name__ == '__main__':
  executor=ProcessPoolExecutor(max_workers=3)
  futures=[]
  for i in range(11):
    future=executor.submit(task,i)
    futures.append(future)
  executor.shutdown(True)
  print('+++>')
  for future in futures:
    print(future.result())

运行结果:

38704 is runing
38704 is runing
38704 is runing
38696 is runing
38696 is runing
38696 is runing
38696 is runing
38696 is runing
38712 is runing
38712 is runing
38712 is runing
+++>
0
1
4
9
16
25
36
49
64
81
100


原文链接:https://www.cnblogs.com/xiangsikai/p/11238085.html

最新更新