使用多处理解决方案实施merge_sort

时间:2018-11-26 03:52:08

标签: python

我试图用多处理解决方案编写合并排序

from heapq import merge
from multiprocessing import Process

def merge_sort1(m):
    if len(m) < 2:
        return m

    middle = len(m) // 2
    left = Process(target=merge_sort1, args=(m[:middle],))
    left.start()
    right = Process(target=merge_sort1, args=(m[middle:],))
    right.start()
    for p in (left, right):
        p.join()
    result = list(merge(left, right))
    return result

使用arr测试

In [47]: arr = list(range(9))                                                                                   
In [48]: random.shuffle(arr)                                                                                    

它重新发布错误:

In [49]: merge_sort1(arr)                                                                                       
TypeError: 'Process' object is not iterable

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

merge(left, right)尝试合并两个进程,而您可能想合并每个进程产生的两个列表。请注意,传递给Process的函数的返回值会丢失;这是一个不同的过程,而不仅仅是一个不同的线程,并且您不能非常轻松地将数据重新整理回父对象,因此默认情况下,Python不会这样做。您需要明确表示自己,并编写这样的渠道。幸运的是,有multiprocessing个数据类型可以为您提供帮助;例如multiprocessing.Pipe

from heapq import merge
import random
import multiprocessing

def merge_sort1(m, send_end=None):
    if len(m) < 2:
        result = m
    else:
        middle = len(m) // 2

        inputs = [m[:middle], m[middle:]]
        pipes = [multiprocessing.Pipe(False) for _ in inputs]
        processes = [multiprocessing.Process(target=merge_sort1, args=(input, send_end))
                    for input, (recv_end, send_end) in zip(inputs, pipes)]
        for process in processes: process.start()
        for process in processes: process.join()
        results = [recv_end.recv() for recv_end, send_end in pipes]

        result = list(merge(*results))

    if send_end:
        send_end.send(result)
    else:
        return result


arr = list(range(9))
random.shuffle(arr)

print(merge_sort1(arr))