Python多处理.dummydeepdish不能一起工作

2024-09-30 22:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对multiprocessing.dummy包和{}写入压缩h5文件有问题。 这就是我要做的:

import deepdish as dd
from multiprocessing.dummy import Pool

def writeThings(args):
    path, np_array = args
    dd.io.save(path, {'arr': np_array}, compression='blosc')

p = Pool(4)
p.map(writeThings, all_np_arrays_and_paths)

当我把deepdish保存命令注释出来时,一切都很好。 似乎dd在Windows上创建了一种损坏的文件,Python检测到这个错误,就会崩溃。有人知道怎么解决这个问题吗?非常感谢你。在


Tags: 文件pathfromimportasnpargsmultiprocessing
1条回答
网友
1楼 · 发布于 2024-09-30 22:28:35

为了澄清,路径是不同的,所以我写不同的文件。然而,这个简单的函数仍然不起作用。但是,如果我将其嵌入到一个threading.Thread类中,该类有一个锁,并用lock.acquire包围dd.io.save命令,并且在编写文件lock.release之后,一切正常。在

下面是每个人的代码片段:

import threading

class writeThings(threading.Thread):
    def __init__(self, args, lock):
        super().__init__()
        self.args = args
        self.lock = lock

    def run(self):
        while self.args:
            path, np_array = self.args.pop()
            # Give this thread unique writing rights
            self.lock.acquire()
            dd.io.save(path, {"arr": np_array}, compression='blosc')
            self.lock.release()

lock = threading.Lock()
n_threads = 4
threads = []

for i in range(n_threads):
    threads.append(writeThings(args_junk[i],lock))

for i in range(n_threads):
    threads[i].start()

for i in range(n_threads):
    threads[i].join()

相关问题 更多 >