在Python

时间:2015-10-21 10:28:16

标签: python multiprocessing

这可能是微不足道的,但对我来说需要很长时间才能在Python中解决。 我试图实现多处理,我使用了池。 我需要传递多个参数。 然后我实现像

def doWork(worker_args):
bbox_list, conf_list, all_rects, w, h, pix_per_w, n = worker_args

sys.stdout.write("w: %d%%   \n" % n )
sys.stdout.flush()

for k in range(h * w):
    conf = conf_list[n][k,1].flatten()[0]
    if conf > 0.75:
       y = int(k / w)
       x = int(k % w)
       bbox = bbox_list[n][k]
       abs_cx = pix_per_w/2 + pix_per_w*x + int(bbox[0,0,0])
       abs_cy = pix_per_h/2 + pix_per_h*y+int(bbox[1,0,0])
       w = bbox[2,0,0]
       h = bbox[3,0,0]
       all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))

然后从主要,我发送

         all_rects = [[[] for x in range(net_config["grid_width"])] for y in range(net_config["grid_height"])]
         pool = multiprocessing.Pool(len(bbox_list))
         worker_args = [(bbox_list, conf_list, all_rects, net_config["grid_width"], net_config["grid_height"], pix_per_w, range(len(bbox_list)))]
         pool.map(doWork, worker_args)

然后打印n in doWork功能错误

TypeError: %d format: a number is required, not list

如果我实施

def doWork(n): and 
in main
pool.map(doWork, range(len(bbox_list)))

有效。从0到5发送数据。

然后我实现像

def doWork(worker_args): and 
in main
worker_args = [(bbox_list, conf_list, all_rects, net_config["grid_width"], net_config["grid_height"], pix_per_w)]
pool.map(doWork, worker_args)

它也有效。 但如果我像第一个例子中那样组合两个,那么我在n处得到错误。 如何以单个数字发送n,而不是列表? 感谢

0 个答案:

没有答案