Eclipse+Pydev:“清理” 按“停止”时不会调用函数?

发布于 2024-08-02 03:43:33 字数 586 浏览 7 评论 0原文

尝试在 Eclipse 中运行此文件

class Try:
   def __init__(self):
       pass
   def __del__(self):
       print 1
a=Try()
raw_input('waiting to finish')

并按停止按钮而不让程序完成不会打印“1”,即永远不会调用 del 方法。 如果我尝试从 shell 运行脚本并执行 ctrl-c\sys.exit "1" 确实会打印出来,即调用 del 。 如果我尝试使用 wait() 也会发生同样的情况:

class A:

    def __enter__(self):
        return None
    def __exit__(self, type, value, traceback):
        print 3


with A():
    print 1
    raw_input('Waiting')
    print 2

如果我在提示时按“停止”,则不会打印“3”

这是为什么? 有办法解决吗?

谢谢,诺姆

Trying to run this file in eclipse

class Try:
   def __init__(self):
       pass
   def __del__(self):
       print 1
a=Try()
raw_input('waiting to finish')

and pressing the stop button without letting the program finish doesn't print "1", i.e the del method is never called. If i try to run the script from the shell and do ctrl-c\sys.exit "1" does get printed i.e del is called.
Same thing if I try to use wait():

class A:

    def __enter__(self):
        return None
    def __exit__(self, type, value, traceback):
        print 3


with A():
    print 1
    raw_input('Waiting')
    print 2

If i press "stop" when prompted, "3" isn't printed

Why is that?
Is there a way around it?

Thanks, Noam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2024-08-09 03:43:33

Python 文档:

<前><代码>__del__(自身)

当实例即将被销毁时调用。 这也称为析构函数。 如果基类具有 __del__() 方法,则派生类的 __del__() 方法(如果有)必须显式调用它,以确保正确删除实例。 请注意,__del__() 方法可以通过创建对实例的新引用来推迟实例的销毁(尽管不推荐!)。 然后,当该新引用被删除时,可以稍后调用它。 不保证为解释器退出时仍然存在的对象调用__del__()方法。

如果要保证调用方法,请使用with 语句

Python docs:

__del__(self)

Called when the instance is about to be destroyed. This is also called a destructor. If a base class has a __del__() method, the derived class's __del__() method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. Note that it is possible (though not recommended!) for the __del__() method to postpone destruction of the instance by creating a new reference to it. It may then be called at a later time when this new reference is deleted. It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.

If you want to guarantee that a method is called use the with-statement

药祭#氼 2024-08-09 03:43:33

在 Eclipse 中直接按下 stop 会终止解释器(尽管它实际上经常失败)。 就像使用 kill/taskkill 一样,进程不知道它的终止。

Ctrl+C 来自维基百科的片段...

Control-C 作为中止命令是
由 UNIX 普及并采用
其他系统。 在 POSIX 系统中,
序列导致活动程序
接收 SIGINT 信号。 如果
程序没有指定如何处理
这种情况下,就终止了。
通常一个程序确实处理
SIGINT 仍会自行终止,
或者至少终止正在运行的任务
里面。

Ctrl+C 是中断程序的控制信号,但正如您可能在该段中间注意到的那样,程序可以指定如何处理该信号。 在 Python 中,Ctrl+C 抛出一个 KeyboardInterrupt 异常,该异常通常会被捕获,然后Python 干净地退出。 即使您使用 Ctrl+C 终止解释器,它也可能会处理它,以便在退出之前清理环境。

我添加以下内容是因为您问“有解决办法吗?”

如果您想停止 raw_input(...) 调用,可以使用 Ctrl+Z 发送 EOF。 我环顾四周,不幸的是,似乎没有办法在 Eclipse 中发送 Ctrl+C/0x03

Pressing stop in Eclipse outright kills the interpreter (though it actually fails fairly often). Like using kill/taskkill, the process is unaware of it's demise.

Ctrl+C snippet from Wikipedia...

Control-C as an abort command was
popularized by UNIX and adopted in
other systems. In POSIX systems, the
sequence causes the active program to
receive a SIGINT signal. If the
program does not specify how to handle
this condition, it is terminated.
Typically a program which does handle
a SIGINT will still terminate itself,
or at least terminate the task running
inside it.

Ctrl+C is a control signal to interrupt the program, but as you may have noticed in the middle of that paragraph, programs can specify how to handle the signal. In Python, Ctrl+C throws a KeyboardInterrupt exception which is normally caught and then Python exits cleanly. Even if you're killing the interpreter with Ctrl+C it may handle it so that it cleans the environment before exiting.

I included the following because you asked "Is there a way around it?"

If you are wanting to stop on raw_input(...) calls, you could use Ctrl+Z to send EOF. I looked around, and there seems to be no way to send Ctrl+C/0x03 in Eclipse, unfortunately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文