如果一段时间不执行任何操作,为什么我的文件被关闭? [英] Why is my file getting closed if I don't do anything with it for a while?

查看:91
本文介绍了如果一段时间不执行任何操作,为什么我的文件被关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始情况:

当前正在处理的应用程序将在特定文件已添加数据并且可以读取时从另一个应用程序接收通知.此刻我有这样的东西:

The application I'm working on at the moment will receive notification from another application when a particular file has had data added and is ready to be read. At the moment I have something like this:

class Foo(object):
    def __init__(self):
        self.myFile = open("data.txt", "r")
        self.myFile.seek(0, 2) #seeks to the end of the file

        self.mainWindow = JFrame("Foo",
                                 defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                                 size = (640, 480))
        self.btn = JButton("Check the file", actionPerformed=self.CheckFile)
        self.mainWindow.add(self.btn)
        self.mainWindow.visible = True

    def CheckFile(self, event):
        while True:
            line = self.myFile.readline()
            if not line:
                break
            print line

foo = Foo()

最终,当在套接字上收到某些消息时,将触发CheckFile().此刻,我正在从JButton触发它.

Eventually, CheckFile() will be triggered when a certain message is received on a socket. At the moment, I'm triggering it from a JButton.

尽管文件没有在程序中的任何其他地方被触摸,而且我没有在文件上使用with,但是当我尝试readline()时,我仍然会得到ValueError: I/O operation on closed file.

Despite the fact that the file is not touched anywhere else in the program, and I'm not using with on the file, I keep on getting ValueError: I/O operation on closed file when I try to readline() it.

初始解决方案:

在试图弄清楚文件何时完全关闭时,我将应用程序代码更改为:

In trying to figure out when exactly the file was being closed, I changed my application code to:

foo = Foo()
while True:
    if foo.myFile.closed == True:
        print "File is closed!"

但是问题就消失了!或者,如果我将其更改为:

But then the problem went away! Or if I change it to:

foo = Foo()
foo.CheckFile()

然后开始的第一个CheckFile()起作用.但是,当我在约5秒后单击按钮时,再次引发异常!

then the initial CheckFile(), happening straight away, works. But then when I click the button ~5 seconds later, the exception is raised again!

将无限循环更改为pass之后,发现一切仍在工作,我的结论是,最初,实例化Foo后无任何事可做,应用程序代码正在结束,foo超出范围,因此foo.myFile超出范围,文件已关闭.尽管如此,swing仍使窗口保持打开状态,当我尝试对未打开的文件进行操作时,这会导致错误.

After changing the infinite loop to just pass, and discovering that everything was still working, my conclusion was that initially, with nothing left to do after instantiating a Foo, the application code was ending, foo was going out of scope, and thus foo.myFile was going out of scope and the file was being closed. Despite this, swing was keeping the window open, which was then causing errors when I tried to operate on an unopened file.

为什么我仍然感到困惑:

奇怪的是,如果foo超出范围,那为什么摆动仍然能够完全挂入foo.CheckFile()?当我单击JButton时,错误不应该是对象或方法不再存在,而不是该方法被成功调用并在文件操作中给出错误的原因吗?

The odd part is, if foo had gone out of scope, why then, was swing still able to hook into foo.CheckFile() at all? When I click on the JButton, shouldn't the error be that the object or method no longer exists, rather than the method being called successfully and giving an error on the file operation?

我的下一个想法是,当JButton尝试调用foo.CheckFile()并发现foo不再存在时,它创建了一个新的Foo,以某种方式跳过了__init__并直接转到了.但是,似乎也不是这样.如果修改Foo.__init__以获取参数,将其存储在self.myNum中,然后将其打印输出到CheckFile()中,则实例化初始对象时传递的值将始终存在.这似乎表明foo完全没有超出范围,这使我回到了我的起点!!!

My next idea was that maybe, when the JButton attempted to call foo.CheckFile() and found that foo no longer existed, it created a new Foo, somehow skipped its __init__ and went straight to its CheckFile(). However, this doesn't seem to be the case either. If I modify Foo.__init__ to take a parameter, store that in self.myNum, and print it out in CheckFile(), the value I pass in when I instantiate the initial object is always there. This would seem to suggest that foo isn't going out of scope at all, which puts me right back where I started!!!

编辑:使用评论中的相关信息整理问题,并删除了许多所说的评论.

Tidied question up with relevant info from comments, and deleted a lot of said comments.

推荐答案

*最初的部分答案(添加到问题中)*

我想我只是想通了.在foo = Foo()之后,由于没有剩下任何代码来保持模块繁忙,因此即使应用程序仍在运行,而Swing窗口仍在工作,该对象似乎仍然不存在.

I think I just figured this out. After foo = Foo(), with no code left to keep the module busy, it would appear that the object ceases to exist, despite the fact that the application is still running, with a Swing window doing stuff.

如果我这样做:

foo = Foo()
while True:
    pass

然后一切都会按照我的预期进行.

Then everything works as I would expect.

尽管如此,我还是很困惑foo.CheckFile()的调用方式.如果问题在于foo.myFile超出范围并被关闭,那么JButton如何调用foo.CheckFile()?

I'm still confused though, as to how foo.CheckFile() was being called at all. If the problem was that foo.myFile was going out of scope and being closed, then how come foo.CheckFile() was able to be called by the JButton?

也许其他人可以提供更好的答案.

Maybe someone else can provide a better answer.

这篇关于如果一段时间不执行任何操作,为什么我的文件被关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆