如何使用Python并发多进程功能,但只调用此会话一次

如何使用Python并发多进程功能,但只调用此会话一次,python,Python,我有一个类似这样的代码: def processImage(filename): with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get

我有一个类似这样的代码:

def processImage(filename):
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')

            #Do Other Stuff Here with "sess" variable like:
            sess.run([abc, xyz, stuff])


def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
                #{executor.map(processImage, filesToProcess):  filesToProcess for filesToProcess in filesToProcess}
                {executor.submit(processImage, filesToProcess): filesToProcess for filesToProcess in filesToProcess}
if __name__ == '__main__':
    main()
但是我只想调用这个代码一次

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        # Definite input and output Tensors for detection_graph
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

但是我需要
processImage
中的
sess
变量。有没有办法,我如何修改这段代码,所以我用detection\u graph.as\u default():调用
,并用tf.Session(graph=detection\u graph)作为sess:
部分只修改一次?

假设检测图和会话对象是可拾取的(即可以序列化以发送到不同的进程),并且可以安全地分发(即,对不同副本的操作是有意义和安全的),您可以这样做

def processImage(f, detection_graph, sess):
   ...

def main():
    with detection_graph.as_default() as dg:
        with tf.Session(graph=detection_graph) as sess:
            with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
                for f in filesToProcess:
                    executor.submit(processImage, f, dg, sess)

代码似乎在某个地方卡住了,请找出原因。非常感谢您提供的解决方案。我们将很快回复您