pythonpickletemporary-files

Python, cPickle dump to tempfile


I have a class:

import os


class TestClass:
    def __init__(self, origin_path):
        self.origin_path = origin_path
        self.new_path = None

    def append_path(self, new_folder):
        self.new_path = os.path.join(self.origin_path, new_folder)
        self.origin_path = self.new_path

I start a python process which creates a new temp file, then starts another python process (different python script). The second python process receives the path to new temporary file, uses the class above, creates a new object and applies method "append_path" to it. Then the second process dumps the class object to the temporary file using cPickle and finishes. Then the first process comes up with loading the class object from temporary file using cPickle.

Process1:

import cPickle
from tempfile import NamedTemporaryFile
from proc2 import create_and_send
import os


temp_file = NamedTemporaryFile(suffix='.ltree', delete=False)
temp_file_name = temp_file.name
create_and_send(temp_file_name)
with open(temp_file_name, 'rb') as received_file:
    sent_obj = cPickle.load(received_file)

os.unlink(temp_file_name)

Process2:

import cPickle
from test_class_send import TestClass


def create_and_send(file_link):
    data = TestClass(r'D:\some\path')
    data.append_path('new_folder_created')
    with open(file_link, 'wb') as pickle_file:
        cPickle.dump(data, pickle_file)

At the end of first process I want to delete the file using os.unlink but I receive WindowsError: [Error 32] at this point.


Solution

  • found:

    1. in process 2 I need to open not with wb mode but with ab

    2. in process 1 I need to close temfile straightaway after creating NamedTemporaryFile

    Works fine with this fixes