1 Star 0 Fork 0

caifan/python_learn

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
process_01.py 663 Bytes
一键复制 编辑 原始数据 按行查看 历史
caifan 提交于 2024-03-14 09:12 +08:00 . 多线程
from multiprocessing import Pool
def add(n):
s = 0
for i in range(n + 1):
s += i
return (n, s)
def calculate():
pool = Pool(processes=4)
result_list = list()
info_dict = dict()
for n in range(10):
result_list.append(pool.apply_async(add, (n,)))
pool.close()
pool.join()
for result in result_list:
k, v = result.get()
info_dict[k] = v
return info_dict
def print_result():
info_dict = calculate()
key_list = sorted(info_dict.keys())
for key in key_list:
print("%s: %s" % (key, info_dict[key]))
if __name__ == "__main__":
# calculate()
print_result()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/sharehappy/python_learn.git
git@gitee.com:sharehappy/python_learn.git
sharehappy
python_learn
python_learn
master

搜索帮助