[Neta] Thread-safe Sleep Sort function in Python (threading)

Following the trend, I created a thread-safe function that returns sort results. Would you like to make something like multiprocessing or joblib?

from threading import Thread, Lock
from time import sleep

def sleep_sort(values):
    sorted_values = []
    lock = Lock()
    def worker(value):
        sleep(value)
        with lock: sorted_values.append(value)
    threads = [Thread(target=worker, args=(value,)) for value in values]
    for thread in threads: thread.start()
    for thread in threads: thread.join()
    return sorted_values

array = [5, 3, 6, 3, 6, 3, 1, 4, 7]
print(sleep_sort(array))

Execution result (this script takes 7 seconds to execute)


[1, 3, 3, 3, 4, 5, 6, 6, 7]

reference: 2012/03/16 Let's write sleep sort generically in Ruby. 2013/05/15 Explanation of sleep sort 2013/10/24 Training sleepsort 2015/06/09 Rust and Sleep sort 2015/06/25 I wrote sleep sort in C # 2015/10/21 I implemented sleep sort in parallel processing of C ++ 2016/11/15 Multi-thread comparison with Sleep Sort (js / TS / VB / C # / C ++ / D / Go / HSP (mist)) 2017/04/24 [[Neta] Sleep Sort with Swift]([Neta] Sleep Sort with Swift) 2017/04/24 [Neta] Sleep Sort with JavaScript 2017/04/26 [Neta] Sleep Sort in Java 2017/04/26 [Neta] Sleep Sort in Python 2017/04/26 sleep sort

Recommended Posts

[Neta] Thread-safe Sleep Sort function in Python (threading)
Bubble sort in Python
Custom sort in Python3
Create a function in Python
Use callback function in Python
Naturally sort Path in Python
ntile (decile) function in python
Asynchronous processing (threading) in python
python in mongodb in descending sort
Nonlinear function modeling in Python
Draw implicit function in python
Immediate function in python (lie)
Sort by date in python
Implement R's power.prop.test function in python
sort warning in the pd.concat function
Function argument type definition in python
Included notation in Python function arguments
Write AWS Lambda function in Python
Measure function execution time in Python
Sort large text files in Python
Function synthesis and application in Python
When specifying multiple keys in python sort
New in Python 3.9 (2)-Sort directed acyclic graphs in Python
python function ①
Precautions when pickling a function in python
[Python] function
OR the List in Python (zip function)
[Python] Sort
Python # sort
Implemented Stooge sort in Python3 (Bubble sort & Quicksort)
python function ②
Automatically register function arguments to argparse in Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
[CpawCTF] Q14. [PPC] Try writing Sort! In Python
A memo about writing merge sort in Python
I implemented the inverse gamma function in python
Python Assertion failure: size.width> 0 && size.height> 0 in function imshow
Sort list elements in a specified order in Python
I tried to implement selection sort in python
[Python] Manipulating elements in a list (array) [Sort]
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
python enumerate function
Meta-analysis in Python
Unittest in python
Python> function> Closure
Epoch in Python
[Python] Generator function
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python