简体   繁体   中英

Python NamedTemporaryFile deleted without closing it

I'm working with ZOPE and Python 2.4 and I have a problem according to a NamedTemporaryFile. I read that the file is deleted when it is closed, but somehow my file is deleted even if it's not. I have a function that writes some xml specifications to a file with Python's threading.Thread. If the Thread is finished, the filename is written to a session variable. I have another function that should open the file when the thread is finished. It's a JS function that checks every 10sec if the status is true. This is working so far, but when I try to open the file by it's filename it is already deleted.

def startWorker(self):    
    ts = time.time()
    self.threadID = ts
    sf = tempfile.NamedTemporaryFile("w+b", prefix=self.threadID, suffix=".zip", dir = "/test/tmp/")
    zf = zipfile.ZipFile(sf, "w", zipfile.ZIP_DEFLATED)
    mythread = self.MyThread(target, self.threadID, zf, sf)
    mythread.join()            
    success = mythread.getSuccess()
    if success:
        self.setSessionVar('status', 'true')
        self.setSessionVar('filename', zf.filename)


class MyThread(threading.Thread):
    def __init__(self, target, threadID, *args):
        self.__threadID = threadID
        self.__target = target
        self.__zf = zf
        self.__sf = sf
        self.__args = args
        threading.Thread.__init__(self, name=self.__threadID)
        self.start()

    def run(self): 
        try:
            self.zfout = self.__target(self.__zf, self.__sf, *self.__args)
            self.__success = True
            self.stop()
        except:
            self.stop()

    def stop(self):                       
        self.__keepAlive = False           

    def getsucces(self):
        return self.__success


def getFile(self): #JS function that is called every 10 sec
    filename = self.getSessionVar('filename', None)
    if self.getSessionVar('status', None) = 'true':
        open(filename) # file is already deleted here
    else:
        #do something

Can anyone give me a hint how to tell Python not to delete the file or help me with how Python is handling the tempfiles? I'm working with python 2.4 so delete=false within NamedTemporaryFile is no option.

You are ignoring the zf and sf objects in MyThread.__init__() ; *args is left untouched.

Because your running thread is not adding additional references to the open file object, by the time the startWorker function finishes there are no more references left to the objects and they are deleted, taking the on-disk file with them.

According to the documentation there are "two" times this file would be deleted;

It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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