Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python进行多处理以查找最大值_Python_Multithreading_Python 2.7_Opencv_Multiprocessing - Fatal编程技术网

使用python进行多处理以查找最大值

使用python进行多处理以查找最大值,python,multithreading,python-2.7,opencv,multiprocessing,Python,Multithreading,Python 2.7,Opencv,Multiprocessing,我正在使用Python 2.7.5和OpenCV。我有一个测试图像,我想在一系列图像中找到最相似的图像。我已经使用OpenCV编写了一个函数,该函数将给出相似点的总数。我得到的相似点越多,图像就越相似。不幸的是,这是一个相当耗时的函数,所以我想并行化我的代码,使其更快 #img is the image that I am trying to find the most number of similar pointswith maxSimilarPts = 0; #testImages is

我正在使用Python 2.7.5和OpenCV。我有一个测试图像,我想在一系列图像中找到最相似的图像。我已经使用OpenCV编写了一个函数,该函数将给出相似点的总数。我得到的相似点越多,图像就越相似。不幸的是,这是一个相当耗时的函数,所以我想并行化我的代码,使其更快

#img is the image that I am trying to find the most number of similar pointswith
maxSimilarPts = 0;

#testImages is a list of testImages
for testImage in testImages:
    #getNumSimilarPts returns the number of similar points between two images
    similarPts = getNumSimilarPts(img, testImage) 

    if similarPts > maxSimilarPts:
        maxSimilarPts = similarPts
如何与python并行执行此操作?任何帮助都将不胜感激。

以下是原始代码的(未经测试的)并行版本。它并行运行5个工人。每个人从输入队列中获取一个图像,计算类似的值,然后将值和图像放入输出队列。完成所有工作后,不再有图像,然后父进程打印最相似图像的(相似性,imageID)

# adapted from Raymond Hettinger
# http://stackoverflow.com/questions/11920490/how-do-i-run-os-walk-in-parallel-in-python/23779787#23779787

from multiprocessing.pool import Pool
from multiprocessing import JoinableQueue as Queue
import os, sys


def parallel_worker():
    while True:
        testImage = imageq.get()
        similarPts = getNumSimilarPts(img, testImage) 
        similarq.put( [similarPts, testImage] )
        imageq.task_done()

similarq = Queue()
imageq = Queue()
for testImage in testImages:
    imageq.put(testImage)

pool = Pool(5)
for i in range(5):
    pool.apply_async(parallel_worker)

imageq.join()
print 'Done'

print max(similarq)
重要提示: 此代码仅在python3上本机运行。要在python2上运行它,必须安装

来自concurrent.futures导入ProcessPoolExecutor
def多进程_max(可编辑,键):
以ProcessPoolExecutor()作为执行器:
返回最大值(执行器映射(lambda项:(项,键(项)),iterable),
key=lambda项:项[1])[0]
背后的想法如下:

昂贵的过程是计算比较项目的关键。 那么,有什么不可以通过多个进程来计算密钥,而只使用一个进程来比较呢

下面是它的工作原理:

创建一个
concurrent.futures.ProcessPoolExecutor
,它是围绕
多处理
模块的一个易于使用的包装器,并提供一个类似内置但同时工作的
map()
函数

然后,从集合中,为每个项创建包含2个元素的元组:原始项(如果它的键是max,则我们要返回的内容)和键,使用传递的
key
函数计算

得到结果后,将其传递给内置的
max()
——但我们有一个问题:集合现在是元组的集合!因此,我们传递一个
key
函数,该函数返回第二项——计算键

最后,由于
max()
返回整个项(其中包括不需要的键),因此我们从其结果中提取第一项(原始项)并返回它

编辑: 在我的控制台中锁定了这个代码之后(空闲;我发现这个问题是因为我也需要它),我认为我的解决方案是错误的:-)

但我错了,不是解决办法此解决方案在解释器中不起作用。发件人:

\uuuu主\uuuu
模块必须可由辅助子流程导入。这 表示
ProcessPoolExecutor
将无法在交互模式下工作 翻译


你可能想看看。它与OpenCV无关。但是它对python的多线程化有很多讨论。