我是 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
分为左右部分)临时创建的两个列表称为L
和R
.
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
我不知道问题的原因。
非常感谢任何帮助!
最佳答案
我不确定您的代码到底出了什么问题,但这是 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]
遗憾的是,它似乎比单线程版本慢很多。然而,这种减速是par对于 course当涉及到 Python 中的多线程计算密集型任务时。
关于python - 如何使用分而治之的多线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245113/