简体   繁体   English

Python 3.x:多进程无法序列化“ ExFileObject”对象

[英]Python 3.x: Multiprocess cannot serialize 'ExFileObject' object

I try to read txt file from the archive(tar) and process file with function. 我尝试从archive(tar)读取txt文件并使用功能处理文件。 I have a lot of files and decided to use multiprocessing lib to speed up calculations. 我有很多文件,因此决定使用multiprocessing lib来加快计算速度。 But I got error "cannot serialize 'ExFileObject' object". 但是我收到错误消息“无法序列化'ExFileObject'对象”。 Below is an example of my code. 以下是我的代码示例。 How to serialize 'ExFileObject' object or how to speed up my calculations with multiprocessing lib? 如何序列化“ ExFileObject”对象或如何通过多处理lib加快计算速度? I am using Python 3.4 我正在使用Python 3.4

import tarfile
from multiprocessing import Process, Manager

def start():
    result = {}
    file_path = '...somepath'
    tar = tarfile.open(file_path, 'r')
    for file in tar:
        extr_file = tar.extractfile(file)
        p = Process(target=straight_calc, args=(extr_file, result)) #thows error
        p.start()
        p.join()
    print(result)

def straight_calc(file, result):
    content = file.readlines()
    for line in content:
        pass

if __name__ == '__main__':
    start()

Your example code works fine for me. 您的示例代码对我来说很好。 I would only add a NoneType check because NoneType has no readlines attribute and tar.extractfile(file) might return a NoneType value. 我只会添加一个NoneType检查,因为NoneType没有readlines属性,并且tar.extractfile(file)可能返回NoneType值。

import tarfile
from multiprocessing import Process, Manager

def start():
    result = {}
    file_path = '...somepath'
    tar = tarfile.open(file_path, 'r')
    for file in tar:
        extr_file = tar.extractfile(file)
        if extr_file is not None:
            p = Process(target=straight_calc, args=(extr_file, result))
            p.start()
            p.join()
    print(result)

def straight_calc(file, result):
    content = file.readlines()
    for line in content:
        pass

if __name__ == '__main__':
    start()

You might try pickling your extr_file with picklestring = pickle.dumps(extr_file) . 您可以尝试使用picklestring = pickle.dumps(extr_file) Pickling extr_file before executing Process should throw a more useful exception. 在执行Process之前对extr_file进行酸洗应该抛出一个更有用的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM