为什么我使用了多进程之后字典会生成三遍?

jhc753951 2018-12-04 09:33:19
# 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函数的判断
...全文
349 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
码仔1号 2018-12-09
  • 打赏
  • 举报
回复
大哥,当前主程序也是一个进程啊,当然是n+1个字典了。。。
jhc753951 2018-12-05
  • 打赏
  • 举报
回复
jhc753951 2018-12-04
  • 打赏
  • 举报
回复
纠正一下,字典生成三次的情况是pool(2),若为pool(1)则会生成两次字典,结尾的n+次是打漏了,应为n+1次

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧