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

问题描述 投票:2回答:1

我是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

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

任何帮助是极大的赞赏!

python python-3.x multithreading python-multithreading
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.