简体   繁体   English

Python NamedTemporaryFile已删除而不关闭它

[英]Python NamedTemporaryFile deleted without closing it

I'm working with ZOPE and Python 2.4 and I have a problem according to a NamedTemporaryFile. 我正在使用ZOPE和Python 2.4,并且根据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. 我有一个函数,可以使用Python的threading.Thread将一些xml规范写入文件。 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. 这是一个JS函数,每隔10秒检查一次状态是否为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? 谁能给我一个提示,告诉他们如何告诉Python不要删除文件,或者帮助我了解Python如何处理临时文件? I'm working with python 2.4 so delete=false within NamedTemporaryFile is no option. 我正在使用python 2.4,因此NamedTemporaryFile中的delete = false是没有选择的。

You are ignoring the zf and sf objects in MyThread.__init__() ; 您将忽略 MyThread.__init__()zfsf对象; *args is left untouched. *args保持不变。

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. 因为您正在运行的线程没有在打开的文件对象上添加其他引用,所以在startWorker函数完成时,不再有剩余的对象引用,它们将被删除,并带有磁盘上的文件。

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). 它将在关闭后立即销毁(包括在垃圾回收对象时的隐式关闭)。

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

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