Python defaultdict 行为可以通过多处理实现吗?

2024-03-19

我不确定这是否可能(希望如此)。我有一个数据集,我通过使用 defaultdict 的过程运行它。 DefaultDict 有一个功能,如果您搜索某些内容并且它不在字典中,它就会被添加(在我的例子中,我正在搜索它们正在添加的值,然后我稍后会搜索这些值,如果它们在字典中,那么我将该值从默认的 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, 1, 1, 1, 1, 1, 1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 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 = 0之前已完成i = 1 and i = 2计算开始。这有时可能意味着i = 0计算写入一个键i = 1使用,但仅在之后i = 1计算已经读取了密钥。 (事实上​​,尽管上例中的表长度相同,但您会注意到order列表不同。所以某物即使不影响最终结果,也会发生不同的情况。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python defaultdict 行为可以通过多处理实现吗? 的相关文章

  • 使用notebook时将jar添加到pyspark

    我正在尝试将 mongodb hadoop 与 Spark 集成 但不知道如何使 IPython 笔记本可以访问这些 jar 这是我想做的 set up parameters for reading from MongoDB via Had
  • goJS 下拉菜单删除项目

    我有简单的 python Flask goJS 图形应用程序 如下所示 节点和链接文本的源是从应用程序的后端加载的 我将它们设置为model modelData像这样的部分 var graphDataString JSON parse di
  • 如何在Python中找到低精度浮点值的原始文本表示?

    我遇到了显示问题floatPython 中的值 从外部数据源加载 它们是 32 位浮点数 但这也适用于较低精度的浮点数 以防万一 这些值是由人类在 C C 中输入的 因此与任意计算值不同 与round数字很 可能not预期的 但不能被忽略
  • Python 如果 kwargs 中的 key 并且 key 为 true

    if force in kwargs and kwargs force is True 感觉应该有更好的方法来编写这个条件 因为我重复了键和变量 假设您确实想检查返回的关键字参数是否is True 这是另一种稍微不同的方式 if kwarg
  • 为什么最新的 Python 3.8.x 版本不提供 Windows 安装程序?

    我需要在Windows计算机上安装Python 3 8并希望使用最新的小版本3 8 12 https www python org downloads release python 3812 官方发布网页提供了源代码的 tarball 文件
  • 如何从数据库模式自动生成示例 Django 应用程序?

    我正在评估概念验证应用程序的框架 该应用程序的生命周期约为 30 天 之后它将被遗忘或完全重写 我已确定要从现有数据库模式自动生成示例应用程序 然后调整视觉设计的某些方面 我看过一个演示红宝石 on Rails 它会为数据库中的每个表自动生
  • tkinter 上的“NoneType”对象没有属性“get”错误[重复]

    这个问题在这里已经有答案了 我最近开始使用 python 3 6 进行编码tkinter并尝试创建我自己的项目repl it 该项目是一个简单的交互式待办事项列表 但是我陷入困境并且无法使该功能正常工作 该函数只是简单地获取条目并将其添加到
  • Pyjnius导入jar文件

    Pyjnius 允许您为 java 类创建 python 包装器 例如 Hardware autoclass org myapp Hardware 有没有办法像这样导入现有的 jar 文件 语法是什么样的 您可以将 jar 添加到 CLAS
  • 点击后 Dash DropDown 关闭

    我不希望下拉菜单在选择值后关闭 我希望它在我的页面上保持打开状态 我正在使用 dcc Dropdown dcc Dropdown id job type options self options placeholder Select one
  • Python中非常大的整数的math.pow是错误的[重复]

    这个问题在这里已经有答案了 我试图通过计算一个整数的非常大的幂来打印一个非常大的数字 尽管我的代码是正确的 但我没有观察到所需的输出 一般来说 Python解释器可以打印系统内存支持的非常大的整数 考虑到这个假设 下面是我正在运行的代码 a
  • 简单 CAE 的问题

    看起来简单的 CAE 不适用于 Carvana 数据集 我正在尝试对 Carvana 数据集进行简单的 CAE 你可以下载它here https www kaggle com c carvana image masking challeng
  • 将Python嵌入到C中——导入模块

    我在使用嵌入式 Python for C 时遇到问题文档 http docs python org extending embedding html 每当我尝试使用导入的模块时 我都会得到 PythonIncl exe 中 0x1e089e
  • 如何在Python中重命名virtualenv?

    我拼错了名字virtualenv使用以下方法初始化它 virtualenv vnev 我实际上打算创建一个名为的环境venv 尝试重命名后vnev文件夹到venv 我发现这并没有提供太多帮助 激活环境的名称仍然重命名旧的vnev mv vn
  • 尝试安装 python 包 Box2D 时出错

    我正在尝试通过 pip 安装 Box2D 软件包的版本 2 3 10 但是 pip 返回以下错误消息 ERROR Could not find a version that satisfies the requirement Box2D 2
  • 在Python中从整个图像中检测表格部分

    我有一张尺寸为 3500x5000 的图像 现在我只想检测整个图像中的表格部分 如果不能直接进行 OCR 处理 则对其进行裁剪和旋转 经过所有搜索后 我想到了使用裁剪图像中的每个单元格的想法https medium com coinmonk
  • Kivy错误(python 2.7):sdl2导入错误

    我尝试在我的 Python 2 7 项目 在 PyCharm Windows 10 环境中 上使用 kivy 但出现以下错误 如果有人可以帮助我吗 谢谢 PS 我多次尝试卸载 重新安装库等 并按照像这样的帖子上的建议进行操作 但它不起作用
  • pygame.image.load 不工作

    我正在尝试为游戏创建世界地图 但是当我尝试将世界地图加载到屏幕上时 命令行告诉我无法执行此操作 这是代码 import sys import pygame from pygame locals import pygame init Surf
  • 如何使 cx-oracle 将查询结果绑定到字典而不是元组?

    这是我的代码 我想找到一种方法将查询结果作为字典列表而不是元组列表返回 看起来 cx oracle 通过部分文档讨论 绑定 来支持这一点 虽然我不知道它是如何工作的 def connect dsn cx Oracle makedsn hos
  • 类型错误:不可散列的类型:pandas 的“切片”

    我有一个 pandas 数据结构 我这样创建 test inputs pd read csv input test csv delimiter 它的形状 print test inputs shape is this 28000 784 我
  • Pandas 将时间序列数据重新采样为 15 分钟和 45 分钟 - 使用多索引或列

    我有一些时间序列数据作为 Pandas 数据框 它从每小时过去 15 分钟和过去 45 分钟 时间间隔为 30 分钟 的观察开始 然后将频率更改为每分钟 我想对数据进行重新采样 以便整个数据帧的频率为每 30 分钟一次 15 点和 45 点

随机推荐

  • WinInet如何使用sock代理

    我想使用 WinInet 访问网页 但使用ocks4 socks5 代理 这是我尝试过的 HINTERNET hOpen InternetOpenA helper USER AGENT INTERNET OPEN TYPE PROXY SO
  • 通过 SSH 远程运行脚本

    我需要从 100 个远程服务器收集用户信息 我们有用于身份验证的公钥 私钥基础设施 并且我已经配置了ssh agent命令转发密钥 这意味着我可以在没有密码提示的情况下登录任何服务器 自动登录 现在我想在所有服务器上运行一个脚本来收集用户信
  • NSMenuItem 未快速启用

    我的项目中有一个 NSMenuItem var statusBar NSStatusBar systemStatusBar var statusItem NSStatusItem NSStatusItem var menuItem NSMe
  • 在 iOS 中使用 Microsoft App Center 环境变量

    我想开源我的 iOS 应用程序 但仍然能够使用 Microsoft App Center 等 CI 系统来构建它 我的应用程序使用我想从开源存储库中隐藏的私有 API 密钥 幸运的是 App Center 提供了环境变量功能 旨在在构建时使
  • OpenSSL 错误消息:错误:0A000126:SSL 例程::读取时出现意外的 eof

    截图在这里 https i stack imgur com 45QV6 png 我使用 YII2 框架用 PHP 构建了一个网站 当我使用 file get contents requestUrl false stream context
  • 在我的所有 vue 组件中全局使用 axios

    我正在 Vue 应用程序和 CLI 中使用 axios 进行测试 我一直在使用 vue resource 只需将其传递给 Vue use VueResource 即可在所有组件上访问它 如何使用 axios 实现此目的 这样我就不必将其导入
  • 如何获取 Flutter 应用程序的屏幕尺寸?

    我以全页模式显示从云存储加载的图像 我可以向图像 URL 提供一个参数来请求特定大小 例如匹配最长的屏幕尺寸 这样 如果屏幕只有 1334 像素 我就不必加载 1920px 图像 如何获取屏幕尺寸作为参数传递 您可以使用MediaQuery
  • mongodb 组和子组计数

    我有以下文档类型 id fA2845ORqeyMUItKXfqZw user 553247ffdc8a4ade4bb09c5e state 2 metadata language en US pipeline api http localh
  • Swift:如何在写入时不断调整 UICollectionViewCell 高度以适应 UITextView?

    下午好 我已经在我的 iOS 应用程序的这个 功能 上苦苦挣扎了几个小时 我需要一些帮助 问题 我应该如何实现 以便当用户输入UITextView尺寸增加 仅底部边距 并且单元格增加其高度以适应UITextView同时动态 我不知道如何解决
  • 如何在 WPF 中创建按钮数组?

    我可以在 Windows 窗体中创建按钮数组 但如何在 WPF xaml 中执行此操作 提前致谢 您不能直接在 XAML 中执行此操作 尽管您可以在代码中执行此操作 其方式与在 Windows 窗体中完全相同 您可以做的是使用数据绑定和 I
  • Qt 文档和可重入

    Qt 文档陈述了关于线程安全和可重入性的内容 http qt project org doc qt 5 0 qtcore threads reentrancy html 注意 Qt 类仅在供多个线程使用时才被记录为线程安全的 如果函数未标记
  • 有意传递文件,我如何检索它

    这就是我要传递的内容 pictureFile is a File Intent intent new Intent context ShowPicActivity class intent putExtra picture pictureF
  • 如何在 IntelliJ IDEA 中设置和运行/调试 ElasticSearch

    我正在尝试了解 ES 的工作原理 但是我无法从 IDE 运行它 我想运行 2 3 3 构建 它仍然使用 Maven 作为其构建系统 但不断出现错误 有人可以告诉我在详细下载存储库后如何在 IDEA 中执行此操作吗 配置等 我从github下
  • Django:无法更改默认语言

    我已经开发了一段时间了en gb语言代码 现在临近发布日期 我打算将默认语言切换为en us 但无论我最初做什么request LANGUAGE CODE被设定为en gb即使对于未注册的用户 仅供参考 我实际上想将时区保留为伦敦 并将默认
  • 如何在 Firefox 中禁用 Ctrl + Shift + C 快捷键?

    Pressing Ctrl Shift C in Firefox opens the developer tools and activates the Pick element tool 当我想要复制某些内容时 我经常错误地使用此快捷方式
  • 如何在java中逐行上传文件到谷歌云存储

    我有以下代码 String fullFileUrl fileUrl Storage storage StorageServiceHolder getStorage BlobId blobId GCSHelper uri2blobId ful
  • 会话数据在本地工作但不在 ipage 服务器上

    我已经尝试调试这个问题几个小时了 但没有成功 我有这两个文件 我的第一个文件 session start SESSION user 1 我的第二个文件 session start print r SESSION echo SESSION u
  • 根据输入将日期添加到框架中

    我有一个 R 数据集的输入变量 用于评估工具列表 它看起来如下 type lt as integer readline prompt Enter a Barcode 我有两列 一列包含通过输入语句找到的信息 一组条形码编号 另一列包含将特定
  • VS导出的模板不适用于新项目

    我已将以下工作项目导出为 VS 模板 该模板使用 ASP NET 5 RC1 https github com mohasi vs spa template https github com mohasi vs spa template 当
  • Python defaultdict 行为可以通过多处理实现吗?

    我不确定这是否可能 希望如此 我有一个数据集 我通过使用 defaultdict 的过程运行它 DefaultDict 有一个功能 如果您搜索某些内容并且它不在字典中 它就会被添加 在我的例子中 我正在搜索它们正在添加的值 然后我稍后会搜索