如何在分而治之中使用多线程?

时间:2018-11-11 01:38:25

标签: python python-3.x multithreading python-multithreading

我是Python新手,一直在尝试使用多线程。关于该主题已经有深入的comment on Stackoverflow,但我仍然有一些疑问。

我的程序的目标是创建和填充一个数组(尽管从技术上讲,我猜它在Python中必须称为“列表”),并通过“分而治之”算法对其进行排序。不幸的是,许多用户似乎混淆了术语“列表”和“数组”,即使它们并不相同。如果我在评论中使用“数组”,请记住,我从各种资源中发布了不同的代码,并且为了尊重原始作者,没有更改其内容。

我用于填充列表count的代码非常简单

#!/usr/bin/env python3
count = []
i = 149
while i >= 0:
    count.append(i)
    print(i)
    i -= 1

此后,我在“分而治之”主题上使用this very handy guide创建了两个要排序的列表,稍后将它们合并。现在我主要关心的是如何在多线程中正确使用这些列表。

earlier mentioned post中,有人认为使用多线程基本上只需要几行代码:

from multiprocessing.dummy import Pool as ThreadPool 
pool = ThreadPool(4)

以及

results = pool.starmap(function, zip(list_a, list_b))

传递多个列表。

我尝试修改代码,但失败了。我函数的参数为​​def merge(count, l, m, r)(用于将列表count分为左侧和右侧),两个临时创建的列表分别称为LR

def merge(arr, l, m, r): 
    n1 = m - l + 1
    n2 = r- m 

    # create temp arrays 
    L = [0] * (n1) 
    R = [0] * (n2) 

但是,每次我运行该程序时,它都会响应以下错误消息:

Traceback (most recent call last):
  File "./DaCcountdownTEST.py", line 71, in <module>
    results = pool.starmap(merge,zip(L,R))
NameError: name 'L' is not defined

我不知道我的问题的原因。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我不确定您的代码到底出了什么问题,但这是the mergeSort code you linked to的多线程版本的完整工作示例:

from multiprocessing.dummy import Pool as ThreadPool 

# Merges two subarrays of arr[]. 
# First subarray is arr[l..m] 
# Second subarray is arr[m+1..r] 
def merge(arr, l, m, r): 
    n1 = m - l + 1
    n2 = r- m 

    # create temp arrays 
    L = [0] * (n1) 
    R = [0] * (n2) 

    # Copy data to temp arrays L[] and R[] 
    for i in range(0 , n1): 
        L[i] = arr[l + i] 

    for j in range(0 , n2): 
        R[j] = arr[m + 1 + j] 

    # Merge the temp arrays back into arr[l..r] 
    i = 0     # Initial index of first subarray 
    j = 0     # Initial index of second subarray 
    k = l     # Initial index of merged subarray 

    while i < n1 and j < n2 : 
        if L[i] <= R[j]: 
            arr[k] = L[i] 
            i += 1
        else: 
            arr[k] = R[j] 
            j += 1
        k += 1

    # Copy the remaining elements of L[], if there 
    # are any 
    while i < n1: 
        arr[k] = L[i] 
        i += 1
        k += 1

    # Copy the remaining elements of R[], if there 
    # are any 
    while j < n2: 
        arr[k] = R[j] 
        j += 1
        k += 1

# l is for left index and r is right index of the 
# sub-array of arr to be sorted 
def mergeSort(arr,l=0,r=None):
    if r is None:
        r = len(arr) - 1

    if l < r: 
        # Same as (l+r)/2, but avoids overflow for 
        # large l and h 
        m = (l+(r-1))//2

        # Sort first and second halves
        pool = ThreadPool(2)
        pool.starmap(mergeSort, zip((arr, arr), (l, m+1), (m, r)))
        pool.close()
        pool.join()

        merge(arr, l, m, r)

这是对代码的简短测试:

arr = np.random.randint(0,100,10)
print(arr)
mergeSort(arr)
print(arr)

产生以下输出:

[93 56 55 60  0 28 17 77 84  2]
[ 0  2 17 28 55 56 60 77 84 93]

可悲的是,它似乎比单线程版本要慢很多。但是,对于Python中的多线程计算绑定任务,par的这种减慢速度是course