Python defaultdict行为可能与多处理吗? [英] Python defaultdict behavior possible with multiprocessing?

查看:62
本文介绍了Python defaultdict行为可能与多处理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可行(希望如此).我有一个使用defaultdict的过程运行的数据集. DefaultDict具有一项功能,如果您搜索某项内容并且不在字典中,它将被添加(在我的情况下,我正在搜索要添加的值,然后再搜索这些值,如果它们在字典中,我将值从默认的false设置为True).可以很容易地工作,没有问题,但是一旦我尝试对其进行多进程处理,我就会开始得到不正确的结果(实际数据/进程非常大,而且我拥有多核硬件,所以为什么不使用它,对吗?).这是我的结果(带有多处理功能的表的大小似乎总是在变化,有时没有多处理功能的表的大小总是相同的,但有时却稍少一些.):

I'm not sure if this is possible(hoping so). I have a dataset that I run through a process that uses defaultdict. DefaultDict has a feature where if you search something and its not in the dictionary it gets added(in my case I'm searching for values they are getting added then I'm searching later on for those values and if they are in the dict then I set the value to True from the default false). Works pretty easily with no problems but I start getting incorrect results once I try to multiprocess this(The real data/process is pretty large and plus I have multicore hardware so why not use it, right?). Here's my results(the size of the table with multiprocessing seems to always be changing, sometimes its the same without multiprocess but often its slightly less.):

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与所有新过程共享的方法,但仍然行不通):

Anyways, Here's some functional code. At the top is the multiprocessed code and at the bottom is without multiprocessed(I figured out how to get the defaultdict to be shared with all the new proceses but still doesn't work):

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)

我希望对此有解决方案.在过去的两周中,我尝试将其运行到数据库中,但是对于非常大的数据集,它太慢了.上面的过程处理了内存中的所有内容(但是仍然需要花费几个小时来运行我的测试数据,这就是为什么我要在其上使用多核的原因).

I hope there's a solution for this. I tried for the past 2 weeks to run this to a database but its too slow with very very large datasets. The above processes handles everything in memory(but still takes a few hours to run on my test data which is why I want to use multicores on it) .

推荐答案

使用标准dict可以轻松复制defaultdict的行为.在这种情况下,在我看来,您可以简单地在test中替换此行:

The behavior of defaultdict is easy to replicate using a standard dict. In this case, it looks to me like you could simply replace this line in test:

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

有这行:

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

在麻烦自定义Manager对象之前,请先看看是否可以使此代码与标准词典一起使用.

See if you can get this code working with a standard dictionary before bothering to customize a Manager object.

但实际上,每个i值都将存储可被处理i + 1的循环访问的值.这意味着每个循环的结果都取决于前一个循环,因此异步方法可能会产生错误.

But actually it seems that each value of i will store values that may be accessed by the loop that handles i + 1. This means that the results for each loop depend on the previous loop, and so an asynchronous approach may produce errors.

要对此进行扩展,请尝试以下代码:

To expand on this, try this code:

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,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, 0, 0, 0]

因此,如您所见,如果您调用p.wait(),则执行顺序是顺序的,如果您未调用,则执行顺序是乱序的.而且由于它是乱序的,您会注意到在i = 1i = 2计算开始之前,并非所有i = 0计算都已完成.有时这可能意味着i = 0计算将写入i = 1使用的密钥,但是仅在i = 1计算已经读取了密钥之后. (确实,尽管在上例中表的长度相同,但是您会注意到order列表的长度不同.因此,有些事情发生了变化,即使它没有不会影响最终结果.)

So as you can see, the order of execution is sequential if you call p.wait() and is out-of-order if you don't. And because it's out of order, you'll notice that not all calculations for i = 0 are complete before the i = 1 and i = 2 calculations begin. That may sometimes mean that an i = 0 calculation writes to a key that i = 1 uses, but only after the i = 1 calculation has already read the key. (And indeed, although the table is the same length in the above example, you'll notice that the length of the order list is different. So something different is happening, even when it doesn't affect the final result.)

这篇关于Python defaultdict行为可能与多处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆