Python - 無法重新排列內核以執行另一個過程

滑桶

簡而言之,我有一個程序,它使用多處理器顯示給定數字範圍的總和。

為了更清楚地說明:

首先,使用for循環,程序按指定範圍(在本例中為1到17)填充列表

然後,程序必須計算指定操作需要多少個列表,然後創建包含那麼多鍵的字典,需要多少個操作。

然後,使用創建的列表之一進行編程,並添加一個相鄰的數字(例如 1+2、3+4...)。下一步是將這些數字添加到由核心數字鍵指定的字典中。

由於17號有問題,因為一開始我什麼都加不起來,所以我把它存到列表中,然後在所有的加法操作之後,我想把它加到之前加法的總和上。

我不知道為什麼,但我的內核似乎沒有“重新安排”自己,每當他們完成任務時,所以我實際上無法執行最後一個操作,這需要我的處理器至少擁有 5 個內核. 目前我在 4 上運行,所以我實際上無法完成第五個過程。我怎樣才能讓我的核心重新安排自己到另一個進程?

def calculate(list, keep_numb, process_number, dictionary):
index = 0
calculate_list = []

while index < len(list):
    if index < len(list) - 1:
        calculate_list.append(list[index] + list[index + 1])
        index += 2

    elif index <= len(list):
        if process_number == 1:
            keep_numb.append(list[index])
        elif len(list) <= 2:
            strings = [str(integer) for integer in dictionary[process_number - 1]]
            to_string = "".join(strings)
            to_int = int(to_string)
            dictionary[process_number] = to_int + keep_numb[0]

        list.pop()

list[:] = []
list.extend(calculate_list)
dictionary[process_number] = calculate_list
calculate_list.clear()
return list

主要的

if __name__ == '__main__':
manager = multiprocessing.Manager()

number_dictionary = manager.dict()

numbers_list = manager.list()
keep_numbers = manager.list()

fill_list(1, 18, numbers_list)
how_many_operations(numbers_list)
create_lists(how_many_lists)
print(numbers_list)
p1 = multiprocessing.Process(target=calculate,
                             args=(numbers_list, keep_numbers, 1, number_dictionary))

p1.start()
p1.join()

p2 = multiprocessing.Process(target=calculate,
                             args=(numbers_list, keep_numbers, 2, number_dictionary))

p2.start()
p2.join()

p3 = multiprocessing.Process(target=calculate,
                             args=(numbers_list, keep_numbers, 3, number_dictionary))

p3.start()
p3.join()

p4 = multiprocessing.Process(target=calculate,
                             args=(numbers_list, keep_numbers, 4, number_dictionary))

p4.start()
p4.join()
p5 = multiprocessing.Process(target=calculate,
                             args=(numbers_list, keep_numbers, 5, number_dictionary))

p5.start()
p5.join()
蒂姆·羅伯茨

這是沒有並行性的任務。這裡的結果是正確的,只有按順序進行才能正確。

def calculate(list, keep_numb, process_number, dictionary):
    index = 0
    calculate_list = []

    while index < len(list):
        if index < len(list) - 1:
            calculate_list.append(list[index] + list[index + 1])
            index += 2

        elif index <= len(list):
            if process_number == 1:
                keep_numb.append(list[index])
            elif len(list) <= 2:
                strings = [str(integer) for integer in dictionary[process_number - 1]]
                to_string = "".join(strings)
                to_int = int(to_string)
                dictionary[process_number] = to_int + keep_numb[0]
            list.pop()

    list[:] = calculate_list
    dictionary[process_number] = calculate_list
#    calculate_list.clear()
    return list

if __name__ == '__main__':
    number_dictionary = {}

    numbers_list = []
    keep_numbers = []

    numbers_list[:] = range(1,19)
    #fill_list(1, 18, numbers_list)
    #how_many_operations(numbers_list)
    #create_lists(how_many_lists)

    i = 0
    while numbers_list:
        print(numbers_list)
        calculate( numbers_list, keep_numbers, i, number_dictionary )
        i += 1

    print(numbers_list)
    print(number_dictionary)

輸出:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[3, 7, 11, 15, 19, 23, 27, 31, 35]
[10, 26, 42, 58]
[36, 100]
[136]
[]
{0: [3, 7, 11, 15, 19, 23, 27, 31, 35], 1: [10, 26, 42, 58], 2: [36, 100], 3: [136], 4: []}

這是一個通過設置各種管道來使用多處理的版本。每個進程將其輸出提供給下一個進程。在這個特定的例子中,這很愚蠢,因為串行解決方案可能表現更好。

import multiprocessing

def calculate(infeed, outfeed, keep_numb, process_number, dictionary):
    index = 0
    calculate_list = []
    pending = []

    while 1:
        n = infeed.get()
        print( "Process", process_number, "read", n )
        if n == None:
            break

        if pending:
            calculate_list.append( pending[0] + n )
            outfeed.put( pending[0] + n )
            pending = []
        else:
            pending = [n]
    if pending:
        calculate_list.append( pending[0] )
        outfeed.put( pending[0] )

    outfeed.put(None)
    dictionary[process_number] = calculate_list

if __name__ == '__main__':
    manager = multiprocessing.Manager()

    number_dictionary = manager.dict()

    numbers_list = []
    keep_numbers = manager.list()

    numbers_list[:] = range(1,19)
    # How many will it take?
    count = 0
    while 1 << count < len(numbers_list):
        count += 1
    print( "Need", count, "processes" )

    feed = multiprocessing.Queue()
    infeed = feed
    for i in range(count):
        outfeed = multiprocessing.Queue()
        p1 = multiprocessing.Process(target=calculate,
                             args=(infeed, outfeed, keep_numbers, i, number_dictionary))
        p1.start()
        infeed = outfeed

    # Shove our numbers into the start of the pipeline.
    # "None" is used as a "done" flag.

    for n in numbers_list:
        feed.put(n)
    feed.put(None)

    # Go read results from the other end of the pipeline.

    while 1:
        n = outfeed.get()
        if n is None: 
            break
        print( n )
    print(number_dictionary)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章