函数版本
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File : d.py
@Time : 2022-09-05 09:17
@Author : 坐公交也用券
@Version : 1.0
@Contact : faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc : 当前文件作用
"""
import threading
from multiprocessing import Pool, cpu_count, Manager
def start(cpus, counter_, list_):
"""
开始处理
:param cpus: CPU 数量
:param counter_: 计数器(字典)
:param list_: 列表
:return:
"""
threads = []
for i in range(cpus):
t = threading.Thread(target=get_one_res, args=(cpus, counter_, i))
t.start()
threads.append(t)
list_.append("CPU ID: %s" % cpus)
for t in threads:
t.join()
def get_one_res(cpus, counter_, j):
"""
:param cpus: Cpu ID
:param counter_: 共享字典
:param j:进程 ID
:return:
"""
# time.sleep(random.randint(1, 10))
# with lock:
a = int(counter_['a']) + 1
counter_['a'] = a
print("Counter:{0}, 进程:{1} 线程:{2}".format(counter_['a'], cpus, j))
if __name__ == '__main__':
pool = Pool(cpu_count())
Counter = Manager().dict()
lock = Manager().Lock()
data_list = Manager().list()
print("开始前列表数量: ", len(data_list))
# 嵌套字典
Counter["a"] = '0'
print("开始前: ", Counter['a'])
for i in range(cpu_count()):
res = pool.apply_async(func=start, args=(i, Counter, data_list,))
pool.close()
pool.join()
print("结束后: ", Counter['a'])
for i in data_list:
print(i)
print("结束后列表数量: ", len(data_list))
效果
/usr/bin/python3.7 /media/liumou/exfat/code/001/f9/d.py
开始前列表数量: 0
开始前: 0
Counter:1, 进程:1 线程:0
Counter:2, 进程:3 线程:0Counter:3, 进程:3 线程:1
Counter:3, 进程:3 线程:2
Counter:4, 进程:4 线程:0
Counter:4, 进程:7 线程:1
Counter:5, 进程:7 线程:0
Counter:6, 进程:6 线程:0
Counter:7, 进程:7 线程:2
Counter:8, 进程:7 线程:3
Counter:9, 进程:5 线程:0
Counter:10, 进程:4 线程:1
Counter:12, 进程:7 线程:4
Counter:12, 进程:5 线程:2
Counter:12, 进程:5 线程:1
Counter:13, 进程:7 线程:5
Counter:14, 进程:4 线程:2Counter:14, 进程:2 线程:0
Counter:14, 进程:7 线程:6
Counter:15, 进程:4 线程:3
Counter:16, 进程:2 线程:1
Counter:17, 进程:5 线程:3
Counter:17, 进程:5 线程:4
Counter:18, 进程:6 线程:2
Counter:18, 进程:6 线程:1
Counter:19, 进程:6 线程:4
Counter:19, 进程:6 线程:5
Counter:19, 进程:6 线程:3
结束后: 19
CPU ID: 0
CPU ID: 1
CPU ID: 3
CPU ID: 4
CPU ID: 7
CPU ID: 2
CPU ID: 5
CPU ID: 6
结束后列表数量: 8
封装类
源码
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File : Pool_共享.py
@Time : 2022-09-05 11:28
@Author : 坐公交也用券
@Version : 1.0
@Contact : faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc : 当前文件作用
"""
import threading
from multiprocessing import Pool, cpu_count, Manager
class Sums:
def __init__(self):
self.dic = {}
self.list_ = []
def start(self, cpus, counter_, list_):
"""
开始处理
:param cpus: CPU 数量
:param counter_: 计数器(字典)
:param list_: 列表
:return:
"""
threads = []
for i in range(cpus):
t = threading.Thread(target=self.get_one_res, args=(cpus, counter_, i))
t.start()
threads.append(t)
list_.append("CPU ID: %s" % cpus)
for t in threads:
t.join()
def get_one_res(self, cpus, counter_, j):
"""
:param cpus: Cpu ID
:param counter_: 共享字典
:param j:进程 ID
:return:
"""
# time.sleep(random.randint(1, 10))
# with lock:
a = int(counter_['number']) + 1
counter_['number'] = a
print("字典值:{0}, 进程:{1} 线程:{2}".format(counter_['number'], cpus, j))
def info(self, name):
"""
打印信息
:param name: 任务名称
:return:
"""
print(f"{name} 列表数量: {len(self.list_)}")
print(f"{name} 字典信息: {self.dic}")
def main(self):
"""
主函数
:return:
"""
# 需要先在主函数定义局部变量
pool = Pool(cpu_count())
dic_ = Manager().dict()
lock = Manager().Lock()
data_list = Manager().list()
# 然后再使用实例变量转换
self.dic = dic_
self.list_ = data_list
# 打印开始实例变量信息
dic_['number'] = '0'
self.info(name='开始前')
for i in range(cpu_count()):
res = pool.apply_async(func=self.start, args=(i, dic_, data_list,))
pool.close()
pool.join()
# 打印结束后实例变量信息
self.info('结束后')
if __name__ == '__main__':
up = Sums()
up.main()
效果
D:\Program\Python310\python.exe E:/code/y/Python_Demo/多线程/Pool_共享.py
开始前 列表数量: 0
开始前 字典信息: {'number': '0'}
Counter:1, 进程:3 线程:0
Counter:2, 进程:3 线程:1
Counter:3, 进程:3 线程:2
Counter:4, 进程:7 线程:0
Counter:5, 进程:7 线程:1
Counter:6, 进程:6 线程:1
Counter:7, 进程:6 线程:2
Counter:8, 进程:4 线程:0
Counter:9, 进程:6 线程:5
Counter:10, 进程:7 线程:2
Counter:11, 进程:4 线程:1
Counter:12, 进程:4 线程:2
Counter:13, 进程:6 线程:4
Counter:14, 进程:5 线程:0
Counter:15, 进程:6 线程:0
Counter:16, 进程:5 线程:1
Counter:17, 进程:7 线程:5
Counter:18, 进程:1 线程:0
Counter:19, 进程:5 线程:2
Counter:20, 进程:6 线程:3
Counter:21, 进程:7 线程:6
Counter:22, 进程:5 线程:3
Counter:23, 进程:5 线程:4
Counter:24, 进程:7 线程:3
Counter:25, 进程:4 线程:3
Counter:26, 进程:7 线程:4
Counter:27, 进程:2 线程:0
Counter:28, 进程:2 线程:1
结束后 列表数量: 8
结束后 字典信息: {'number': 28}