簡體   English   中英

多處理可以實現Python defaultdict行為嗎?

[英]Python defaultdict behavior possible with multiprocessing?

我不確定這是否可行(希望如此)。 我有一個數據集,我通過使用defaultdict的進程運行。 DefaultDict有一個功能,如果你搜索一些東西,而不是在字典中它被添加(在我的情況下,我正在搜索它們被添加的值,然后我正在搜索這些值,如果它們在dict然后我從默認的false設置值為True)。 工作非常輕松沒有問題,但是一旦我嘗試多處理這一點,我開始得到不正確的結果(真正的數據/過程相當大,而且我有多核硬件所以為什么不使用它,對吧?)。 這是我的結果(具有多處理的表的大小似乎總是在變化,有時在沒有多進程的情況下是相同的,但通常稍微少一點。):

size of Table(with multiprocesing) is: 398
total number of true(with multiprocesing) is  0
size of Table(without multiprocesing) is  402
total number of true(without multiprocessing) is  250

無論如何,這是一些功能代碼。 頂部是多處理代碼,底部沒有多處理(我想出了如何讓defaultdict與所有新程序共享,但仍然不起作用):

from multiprocessing import Pool
from multiprocessing.managers import BaseManager, DictProxy
from collections import defaultdict

class MyManager(BaseManager):
    pass

MyManager.register('defaultdict', defaultdict, DictProxy)

def test(i,x, T):
    target_sum = 100
    # T[x, i] is True if 'x' can be solved
    # by a linear combination of data[:i+1]
    #T = defaultdict(bool)           # all values are False by default
    T[0, 0] = True                # base case

    for s in range(target_sum + 1): #set the range of one higher than sum to include sum itself
            #print s
            for c in range(s / x + 1):  
                if T[s - c * x, i]:
                    T[s, i + 1] = True


data = [2,5,8]                
pool = Pool(processes=2)
mgr = MyManager()
mgr.start()
T = mgr.defaultdict(bool)
T[0, 0] = True 
for i, x in enumerate(data):    # i is index, x is data[i]
    pool.apply_async(test, (i,x, T))
pool.close()
pool.join()
pool.terminate()


print 'size of Table(with multiprocesing) is:', len(T)
count_of_true = []
for x in T.items():
    if T[x] == True:
       count_of_true.append(x)
print 'total number of true(with multiprocesing) is ', len(count_of_true)


#now lets try without multiprocessing
target_sum = 100
# T[x, i] is True if 'x' can be solved
# by a linear combination of data[:i+1]
T1 = defaultdict(bool)           # all values are False by default
T1[0, 0] = True                # base case


for i, x in enumerate(data):    # i is index, x is data[i]
    for s in range(target_sum + 1): #set the range of one higher than sum to include sum itself
            for c in range(s / x + 1):  
                if T1[s - c * x, i]:
                    T1[s, i + 1] = True

print 'size of Table(without multiprocesing) is ', len(T1)

count = []
for x in T1:
    if T1[x] == True:
        count.append(x)

print 'total number of true(without multiprocessing) is ', len(count)

我希望有一個解決方案。 我在過去的兩周內嘗試將其運行到數據庫,但是對於非常大的數據集來說它太慢了。 上面的過程處理內存中的所有內容(但仍然需要幾個小時來運行我的測試數據,這就是我想在其上使用多核的原因)。

defaultdict的行為很容易使用標准dict進行復制。 在這種情況下,我認為你可以簡單地在test替換這一行:

if T[s - c * x, i]:

用這一行:

if T.get((s - c * x, i), False):

在開始自定義Manager對象之前,看看是否可以使用標准字典使用此代碼。

但實際上似乎i每個值都將存儲可以由處理i + 1的循環訪問的值。 這意味着每個循環的結果取決於前一個循環,因此異步方法可能會產生錯誤。

要擴展此功能,請嘗試以下代碼:

from multiprocessing import Pool
from multiprocessing.managers import BaseManager, DictProxy, ListProxy
from collections import defaultdict

class MyManager(BaseManager):
    pass

MyManager.register('defaultdict', defaultdict, DictProxy)
MyManager.register('list', list, ListProxy)

def test(i,x,T, order):
    target_sum = 100
    # T[x, i] is True if 'x' can be solved
    # by a linear combination of data[:i+1]
    #T = defaultdict(bool)          # all values are False by default
    T[0, 0] = True                  # base case
    for s in range(target_sum + 1): # set the range of one higher 
                                    # than sum to include sum itself
        for c in range(s / x + 1):
            if T[s - c * x, i]:
                T[s, i + 1] = True
                order.append(i)

def setup():
    mgr = MyManager()
    mgr.start()
    run_order = mgr.list()
    T = mgr.defaultdict(bool)
    T[0, 0] = True
    data = [2,5,8]
    return data, T, run_order

def async_loop(data, func, output, run_order, wait=False):
    pool = Pool(processes=6)
    for i, x in enumerate(data):    # i is index, x is data[i]
        p=pool.apply_async(func, (i, x, output, run_order))
        if wait:
            p.wait()
    pool.close()
    pool.join()
    pool.terminate()

def output_size(output, run_order, wait):
    wait = 'out' if wait else ''
    print 'size of Table (with{0} multiprocesing) is: {1}'.format(
        wait, len(output))
    count_of_true = []
    for (x, result) in output.items():
        if output[x] == True:
            count_of_true.append(x)
    print 'total number of true (with{0} multiprocesing) is: {1}'.format(
        wait, len(count_of_true))
    print 'run order is: {0}'.format(run_order)

data, table, run_order = setup()
async_loop(data, test, table, run_order, wait=True)
output_size(table, run_order, True)
data, table, run_order = setup()
async_loop(data, test, table, run_order, wait=False)
output_size(table, run_order, false)

輸出如下:

size of Table (without multiprocesing) is: 402
total number of true (without multiprocesing) is: 250
run order is: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 0, 1, 2, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 0, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0]

正如您所看到的,如果您調用p.wait() ,則執行順序是順序的,如果不調用,則執行順序是無序的。 並且因為它不按順序,你會注意到在i = 1i = 2計算開始之前, i = 0所有計算都沒有完成。 這有時可能意味着i = 0計算寫入i = 1使用的密鑰,但僅在i = 1計算已經讀取密鑰之后。 (事實上,雖然該表是在上面的例子一樣長,你會注意到的長度order列表是不同的,所以不同的東西正在發生的事情,即使它不會影響最終結果)。

我做了一個快速檢查,我懷疑,子進程正在創建它自己的T版本。您需要設置一個全局變量並讓管理器更新變量。

我在測試函數中添加了這個以查看id T是什么:

T[0, 0] = True                # base case
filename = "test."+str(i)
with open(filename,  "w" ) as f:
    f.write( "address of T %x\n" % id(T) )
f.close()

T 955bd0的T 955550地址的T 823f50地址

因此,當孩子完成時,父母永遠不會得到更新。

我將使用它來設置一個全局或進程共享的字典。

我修了幾件事

我從函數調用中刪除了T,這會殺死你定義為manager.defaultdict(bool)的進程var

編輯:實際上,我剛剛意識到,T是全局因為沒有def main,我將T恢復回函數調用。 對於那個很抱歉。 :)

編輯2:我還在同步后添加了p.wait()。 我想這可能是你看到滴劑的地方。 我注意到同樣的下降,但添加p.wait看起來已經停止了孩子們的滴。

編輯3:將p.wait()更改為p.get(timeout = 5)

您只需傳遞功能參數,而不是全局變量。 此外,在循環中,T完成后的結果是:

T = defaultdict(,{(7,3):True,(90,0):False,... etc})

所以我改變了for循環以獲取鍵值。

size of Table(with multiprocesing) is: 402
total number of true(with multiprocesing) is  250
size of Table(without multiprocesing) is  402
total number of true(without multiprocessing) is  250
    from multiprocessing import Pool
    from multiprocessing.managers import BaseManager, DictProxy
    from collections import defaultdict

    class MyManager(BaseManager):
        pass

    MyManager.register('defaultdict', defaultdict, DictProxy)

    def test(i,x,T):
        target_sum = 100
        # T[x, i] is True if 'x' can be solved
        # by a linear combination of data[:i+1]
        #T = defaultdict(bool)           # all values are False by default
        T[0, 0] = True                # base case
        for s in range(target_sum + 1): #set the range of one higher than sum to include sum itself
            for c in range(s / x + 1):  
                if T[s - c * x, i]:
                    T[s, i + 1] = True



    mgr = MyManager()
    mgr.start()
    T = mgr.defaultdict(bool)

    T[0, 0] = True 
    data = [2,5,8]                
    pool = Pool(processes=2)

    for i, x in enumerate(data):    # i is index, x is data[i]
        p=pool.apply_async(test,(i,x,T))
        p.get(timeout=5)
    pool.close()
    pool.join()
    pool.terminate()

    print 'size of Table(with multiprocesing) is:', len(T)
    count_of_true = []
    for (x, result) in T.items():
        if T[x] == True:
           count_of_true.append(x)
    print 'total number of true(with multiprocesing) is ', len(count_of_true)

    #==========================
    #now lets try without multiprocessing
    target_sum = 100
    # T[x, i] is True if 'x' can be solved
    # by a linear combination of data[:i+1]
    T1 = defaultdict(bool)           # all values are False by default
    T1[0, 0] = True                # base case


    for i, x in enumerate(data):    # i is index, x is data[i]
        for s in range(target_sum + 1): #set the range of one higher than sum to include sum itself
                for c in range(s / x + 1):  
                    if T1[s - c * x, i]:
                        T1[s, i + 1] = True

    print 'size of Table(without multiprocesing) is ', len(T1)

    count = []
    for x in T1:
        if T1[x] == True:
            count.append(x)

    print 'total number of true(without multiprocessing) is ', len(count)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM