共享python多处理数组作为对象实例的属性?

2024-06-02 08:21:09 发布

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

我试图读取MIDI文件,并将注释添加到具有多处理pool.map()的列表中,因为有数千个MIDI文件。我用python类开发这个项目,如下所示。在

class NoteProcessor:

    def __init__(self):
        self.note_sequences = list()
        self.notes = set()
        .....

    def process_midi(self, df):
        PATH_ROOT = 'clean_midi/'
        for row in tqdm(df.itertuples(), total=df.shape[0]):
            notes = set()
            path = PATH_ROOT + row.path + '/' + row.song + '.mid'
            try:
                midi = converter.parse(path)
                elements = instrument.partitionByInstrument(midi)
                encoded_notes = self.arrange_notes(elements)
                if len(encoded_notes) != 0:
                    #self.note_sequences should be shared array
                    self.note_sequences.append(encoded_notes)
                else:
                    self.df = self.df.drop(row[0])
            except Exception as e:
                self.df = self.df.drop(row[0])
                logging.error("MIDI Error " + str(e))

主脚本:

^{pr2}$

由于note_sequences不是共享数组,所以它没有用。我研究了python多处理数组,找到了一些答案answer1answer2。但是我把共享数组作为类属性混淆了,当我将self.note_sequences = list()替换为self.note_sequences = multiprocessing.Array(c_char_p,df.shape[0])时,会出现{}错误。如何解决这个问题?在


Tags: 文件pathselfdfdef数组listmidi