【问题标题】:Concurrent quick sort (multithreading)并发快速排序(多线程)
【发布时间】:2016-04-13 10:37:50
【问题描述】:

我正在尝试使用threading 进行快速排序并发。但是当我使用我的线程方法运行代码时,它只会递归地为所有分区运行相同的线程。

这是我尝试过的。

from threading import Thread
import threading
import time
import thread

def qsort(sets,left,right):
    i = left
    j = right
    pivot = sets[(left + right)/2]
    temp = 0
    while(i <= j):
         while(pivot > sets[i]):
             i = i+1
         while(pivot < sets[j]):
             j = j-1
         if(i <= j):
             temp = sets[i]     
             sets[i] = sets[j]
             sets[j] = temp
             i = i + 1
             j = j - 1
    if (left < j):
        thread = Thread(target = qsort(sets,left,j))
        name =  threading.current_thread() 
        printp(sets,elements,name)
    if (i < right):
        thread1 = Thread(target=qsort(sets,i,right))
        name =  threading.current_thread()
        printp(sets,elements,name)
    return sets

【问题讨论】:

  • 首先,您不会启动任何已创建的线程。其次(实际上应该是第一个),由于臭名昭著的GIL,在 Python 中,多线程不会给您带来任何性能提升。
  • 我也使用了 thread.start()。没有成功。
  • 回到你因为 GIL 而没有在 python 中使用多线程的原因。我真的不在乎,因为我在 5 天内有一个实用程序,并且问题陈述是并发快速排序。
  • 所以我应该使用多进程而不是多线程吗?
  • 嗯,首先你当然可以让它与线程一起工作,尽管没有看到你如何启动线程并等待它们完成,所以很难提出任何建议。

标签: python multithreading quicksort


【解决方案1】:

(除了@bereal 在问题下的评论中指出的问题)您看到的问题的根本原因(它们都在同一个线程中运行)是您滥用了class Thread 的第二个参数“目标” ,它应该是一个可调用的对象。

下面是固定代码:

from threading import Thread
import threading
import time
import thread

def qsort(sets,left,right):

    print("thead {0} is sorting {1}".format(threading.current_thread(), sets[left:right]))

    i = left
    j = right
    pivot = sets[(left + right)/2]
    temp = 0
    while(i <= j):
         while(pivot > sets[i]):
             i = i+1
         while(pivot < sets[j]):
             j = j-1
         if(i <= j):
             temp = sets[i]     
             sets[i] = sets[j]
             sets[j] = temp
             i = i + 1
             j = j - 1

    lthread = None
    rthread = None

    if (left < j):
        lthread = Thread(target = lambda: qsort(sets,left,j))
        lthread.start()

    if (i < right):
        rthread = Thread(target=lambda: qsort(sets,i,right))
        rthread.start()

    if lthread is not None: lthread.join()
    if rthread is not None: rthread.join()
    return sets


'''testing below'''
ls = [1,3,6,9,1,2,3,8,6]
res = qsort(ls, 0, len(ls) - 1)
print(res)

输出:

thead <_MainThread(MainThread, started 49900)> is sorting [1, 3, 6, 9, 1, 2, 3,8]
thead <Thread(Thread-1, started 38136)> is sorting [3, 6, 9, 1, 2, 3, 8]
thead <Thread(Thread-2, started 42024)> is sorting [6, 9, 3, 2, 3, 8]
thead <Thread(Thread-3, started 36344)> is sorting [9, 3, 6, 3, 8]
thead <Thread(Thread-4, started 47684)> is sorting [6, 3]
thead <Thread(Thread-5, started 27760)> is sorting [6, 8]
[1, 1, 2, 3, 3, 6, 6, 8, 9]

【讨论】:

  • 啊,对了,他在调用函数……但除此之外,序列thread.start(); thread.join()完全消除了并发,因为它会先等待'左'线程,然后启动并等待'正确的'线程,而不是同时产生它们。
  • 排序前 11 4 8 2 6 1 7 <_mainthread> 1 2 8 4 6 11 7 <_mainthread> 1 2 4 6 8 11 7 1 2 4 6 7 8 11 <_mainthread> 1 2 4 6 7 8 11 <_mainthread> 1 2 4 6 7 8 11 排序后2 4 6 7 8 11
  • @yudhveersingh,您得到的观察结果(基于我上面的代码)是因为您的转储线程名称代码也不正确。让我更新我的代码(再次)
【解决方案2】:

Python 中没有线程并行执行。使用池或进程。叉子还是可以的。 亚历克斯

【讨论】:

  • 亲爱的 Bonoit,你知道 Python 中的 GIL 是什么吗?请阅读许多资源,例如stackoverflow.com/questions/17130975/python-vs-cpython 这不是因为您可以创建在 Python 中进行并行处理的线程。在 Python 中没有线程并行执行。另见hackernoon.com/…
  • Benoit 的另一条评论。让我们为每个人(你和我)写一个快速排序的 Python 程序,你用你的线程,我用我的进程(我的被阅读,我教它!)。速度较慢的一个会向另一个发送糖糖!好吗?
  • 好吧,我投降。但它似乎与 Cpython 实现而不是语言本身相关联,所以它主要取决于他的设置(好吧,我承认我只是在这里寻找一个借口)。我过去用线程池做了一些测试,我注意到一些改进,但它们只是因为我正在使用的模块中的time.sleep(等待文件的出现),所以它不会在这里帮助。我已经删除了我的评论,并感谢您发送的资源,(也许下次直接把它们作为答案,对于像我这样的下一个愚蠢的人)。
猜你喜欢
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 2017-12-26
相关资源
最近更新 更多