为什么我使用了多进程之后字典会生成三遍?
# coding=utf-8
from time import ctime
# print('all start at', ctime())
from xlrd import open_workbook
from xlutils.copy import copy
from Dictionaries import all_point # 将其做成函数
from Record import record_ok_loufang, record_NG_guosha, record_over
from multiprocessing import Pool
# ---------------------------------------------------------------------------------------------------------
# 打开表格生成表单
rb = open_workbook(r'.\result.xls')
workbook = copy(rb)
worksheet = workbook.get_sheet(0)
headers = ['WIP ID', '项目', '49-50']
m, n = 49, 50
for i in range(1, 50):
headers.append('{}-{}'.format(m - 1, n - 1))
m -= 1
n -= 1
for i in range(len(headers)):
# 写入标题列表数据
worksheet.write(0, i, headers[i])
# ---------------------------------------------------------------------------------------------------------
# 计算中心点函数
def center(a, b):
c = (a + int(b / 2))
return c
# ---------------------------------------------------------------------------------------------------------
# 主要判断函数
def judge(k):
print('helloworld')
# print('k= ', k, 'start at', ctime())
ok = [0] * 50
NG = [0] * 50
over = [0] * 50
loufang = [0] * 50
guosha = [0] * 50
x, y = -600, -400
while (center(y, k) <= 600):
while (center(x, k) <= 900):
key = str(center(x, k)) + ' ' + str(center(y, k))
if key in all_point:
if all_point[key][0] != 2:
if all_point[key][0] != 1: # 若合格
record_ok_loufang(x, y, k, ok, loufang, over)
else:
record_NG_guosha(x, y, k, NG, guosha, over)
else:
record_over(x, y, k, over)
else:
record_over(x, y, k, over)
x += k # x坐标向左移动k个单位
y += k
x = -600
p = 2
result = []
result.append(list(reversed(ok))) # ok
result.append(list(reversed(NG))) # ng
result.append(list(reversed(over))) # over
result.append(list(reversed(loufang))) # loufang
result.append(list(reversed(guosha))) # guosha
# print('k= ', k, 'start at', ctime())
return result
# ---------------------------------------------------------------------------------------------------------
# 多进程开始
if __name__ == '__main__':
pool = Pool(1)
results = []
for i in range(1, 9):
print('this')
res = pool.apply_async(judge, (i,))
results.append(res.get())
pool.close()
pool.join()
line = 1
p = 2
for i in range(8):
for j in range(5):
for k in range(50):
worksheet.write(line, p + k, results[i][j][k])
line += 1
workbook.save('result.xls') # 保存工作簿
print('over at ', ctime())
以下是两个自定义函数包的内容:1.Dictionaries.py:
import os
import pandas as pd
from time import ctime
print('字典开始', ctime())
path = os.path.join(os.getcwd(), r'.\XG807930200.csv')
f = open(path, encoding='ISO-8859-1') # 报UnicodeDecodeError: 'utf-8' 错就将utf-8改成ISO-8859-1
data = pd.read_csv(f, low_memory=False)
# 生成迭代器
Vf3 = iter(data.ix[7:, 10])
Ir1 = iter(data.ix[7:, 4])
Vf0 = iter(data.ix[7:, 15])
X = iter(data.ix[7:, 13])
Y = iter(data.ix[7:, 14])
all_point = {}
try:
while True:
vf3 = next(Vf3)
ir1 = next(Ir1)
vf0 = next(Vf0)
x = next(X)
y = next(Y)
key = (str(x) + ' ' + str(y))
if 2 <= float(vf3) <= 5: # 判断该点是否有效(条件1)
if 0 <= float(ir1) <= 0.2 and 1.8 <= float(vf0) <= 3.0: # 判断该点是否合格(条件2)
value = 0
else:
value = 1
else:
value = 2
all_point.setdefault(key, []).append(value) # 加入字典
except StopIteration:
pass
print('字典结束', ctime())
2.Record
from Dictionaries import all_point
import math as mt
def count_l(x, y):
a = int(mt.sqrt((((int(x) - 136) * 0.083) ** 2 + ((int(y) - 129) * 0.121) ** 2)))
if y > 129:
ring = int((129 - y) * 0.121 / 0.96)
if a < ring:
a = ring
return a
def record_ok_loufang(x, y, k, arr1, arr2, arr3):
for i in range(0, k): # 计算被参照点坐标
for j in range(0, k):
xc = x + j
yc = y + i
key_temp = str(xc) + ' ' + str(yc)
if key_temp in all_point:
if all_point[key_temp][0] != 2:
arr1[int(count_l(xc, yc))] += 1
if all_point[key_temp][0] != 1: # 判断该点是否合格(条件2)
pass
else:
arr2[int(count_l(xc, yc))] += 1 # 记录漏放,漏放
else:
arr3[int(count_l(xc, yc))] += 1
return arr1, arr2, arr3
def record_NG_guosha(x, y, k, arr1, arr2, arr3):
for i in range(0, k): # 计算被参照点坐标
for j in range(0, k):
xc = x + j
yc = y + i
key_temp = str(xc) + ' ' + str(yc)
if key_temp in all_point:
if all_point[key_temp][0] != 2:
arr1[int(count_l(xc, yc))] += 1 # 记录相应环数
if all_point[key_temp][0] != 1: # 判断该点是否合格(条件2)
arr2[int(count_l(xc, yc))] += 1 # 记录过杀环数
else:
arr3[int(count_l(xc, yc))] += 1
return arr1, arr2, arr3
def record_over(x, y, k, arr1):
for i in range(0, k): # 计算被参照点坐标
for j in range(0, k):
xc = x + j
yc = y + i
key_temp = str(xc) + ' ' + str(yc)
if key_temp in all_point:
arr1[int(count_l(xc, yc))] += 1 # 记录相应环数
return arr1
程序可以正常运行,只是不知道为何进程数量为n的时候字典就会被生成n+次?这个动作占用了太多的时间,且字典生成之前不会进行judge函数的判断