__del__模式? [英] __del__ pattern?

查看:80
本文介绍了__del__模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确保在给定时间我的机器上只有一个我的python类的实例。 (不在翻译中 - 那将是b $ b只是一个单身人士 - 但在机器上。)这些实例是创造和销毁的
,但只有一个一次。


因此,当我的类被实例化时,我创建了一个小锁文件,并且我有一个__del__方法删除了锁文件。不幸的是,

似乎是某些情况,我的锁文件没有被删除

。然后,所有需要特殊的工作课程开始

排队请求,我在半夜接听电话。


是否有比使用__del__方法更好的模式?我需要绝对肯定两件事:


1)机器上只有一个我的特殊类的实例

时间。

2)如果我的特殊类因任何原因被销毁,我需要能够创建另一个类的实例。

I need to ensure that there is only one instance of my python class on
my machine at a given time. (Not within an interpreter -- that would
just be a singleton -- but on the machine.) These instances are
created and destroyed, but there can be only one at a time.

So when my class is instantiated, I create a little lock file, and I
have a __del__ method that deletes the lock file. Unfortunately, there
seem to be some circumstances where my lock file is not getting
deleted. Then all the jobs that need that "special" class start
queueing up requests, and I get phone calls in the middle of the night.

Is there a better pattern to follow than using a __del__ method? I
just need to be absolutely, positively sure of two things:

1) There is only one instance of my special class on the machine at a
time.
2) If my special class is destroyed for any reason, I need to be able
to create another instance of the class.

推荐答案

>因此,当我的类被实例化时,我创建了一个小锁文件,而我
> So when my class is instantiated, I create a little lock file, and I
有一个删除锁文件的__del__方法。不幸的是,似乎有些情况下我的锁文件没有被删除。
have a __del__ method that deletes the lock file. Unfortunately, there
seem to be some circumstances where my lock file is not getting
deleted.




也许解释器因信号而死...在这种情况下,__del__

不会被调用。


您可以尝试''flock'',而不是锁定文件。

导入fcntl


类Test1(对象):


def __init __(自我):

self.lock = open(''/ var / tmp / test1'',''w'')

fcntl.flock(self.lock.fileno(),fcntl.LOCK_EX)

打印''锁定获取!''


def __del __(自我):

fcntl.flock(self.lock.fileno( ),fcntl.LOCK_UN)

self.lock.close()

在这种情况下,如果解释器死掉,操作系统会释放锁。 />

如果您尝试在同一个解释器中创建另一个实例

或其他实例,则调用将在__init__中阻止。您可以将其更改为

而不是提高异常。


BranoZ



Maybe the interpreter died by the signal.. in that case the __del__
is not called.

You can try ''flock'', instead of lock files.

import fcntl

class Test1(object):

def __init__(self):
self.lock=open(''/var/tmp/test1'', ''w'')
fcntl.flock(self.lock.fileno(), fcntl.LOCK_EX)
print ''Lock aquired!''

def __del__(self):
fcntl.flock(self.lock.fileno(), fcntl.LOCK_UN)
self.lock.close()

In this case, if interpreter dies, the lock is released by OS.

If you try to create another instance in the same interpreter
or another, the call will block in __init__. You can change it to
raise an exception instead.

BranoZ


Chris Curvey < CC ***** @ gmail.com>写道:
"Chris Curvey" <cc*****@gmail.com> writes:
我需要确保在给定时间我的机器上只有一个我的python类实例。


我建议您修改您的要求,以确保

只有一个有效在任何时候你的班级的实例(或者类似的东西是
),然后使用try:finally:blocks来确保你的

锁被删除。

是否有比使用__del__方法更好的模式?我只需要绝对肯定两件事:

1)在机器上只有一个我的特殊课程的实例。
2)如果我的特殊课程因任何原因被销毁,我需要能够创建该课程的另一个实例。
I need to ensure that there is only one instance of my python class on
my machine at a given time.
I recommend modifying your requirements such that you ensure that
there is only one "active" instance of your class at any one time (or
something like that), and then use try:finally: blocks to ensure your
locks get removed.
Is there a better pattern to follow than using a __del__ method? I
just need to be absolutely, positively sure of two things:

1) There is only one instance of my special class on the machine at a
time.
2) If my special class is destroyed for any reason, I need to be able
to create another instance of the class.




另外提到的海报,如果你的过程以一种不允许的方式被杀死,你还需要弄清楚你要做什么?b
$ b最后阻止运行(这与Python没什么关系。


干杯,

mwh


-

上述评论可能非常具有感染力。对于你的

保护,它已被腐烂了两次。

- JWhitlock的签名。 on slashdot



As another poster mentioned, you also need to work out what you''re
going to do if your process gets killed in a way that doesn''t allow
finally blocks to run (this doesn''t have much to do with Python).

Cheers,
mwh

--
The above comment may be extremely inflamatory. For your
protection, it has been rot13''d twice.
-- the signature of "JWhitlock" on slashdot


2005年8月15日星期一,Chris Curvey写道:
On Mon, 15 Aug 2005, Chris Curvey wrote:
是否有比使用a更好的模式__del__方法?我只需要绝对肯定两件事:
Is there a better pattern to follow than using a __del__ method? I just
need to be absolutely, positively sure of two things:




我以前见过的一个老hack是创建一个服务器套接字 - 即,制作一个

套接字并将其绑定到一个端口:


导入套接字


class SpecialClass:

def __init __(self):

self.sock = socket.socket()

self.sock.bind(("",4242) ))

def __del __(自我):

self.sock.close()


无论如何都是这样的。


任何时候只能将一个套接字绑定到给定端口,因此第二个

的SpecialClass实例将从绑定调用中获取异常,并且

将是死产。虽然这是一个非常苛刻的黑客攻击 - 你的机器上有一个开放式端口没有任何理由,你最终会获得
。如果你在

unix上运行,你可以尝试使用unix-domain套接字;我不确定那些绑定语义是什么




我认为Brano建议使用flock是一个更好的解决方案。


tom


-

杜松子酒是男人的意思;让我们喝酒骚乱!



An old hack i''ve seen before is to create a server socket - ie, make a
socket and bind it to a port:

import socket

class SpecialClass:
def __init__(self):
self.sock = socket.socket()
self.sock.bind(("", 4242))
def __del__(self):
self.sock.close()

Something like that, anyway.

Only one socket can be bound to a given port at any time, so the second
instance of SpecialClass will get an exception from the bind call, and
will be stillborn. This is a bit of a crufty hack, though - you end up
with an open port on your machine for no good reason. If you''re running on
unix, you could try using a unix-domain socket instead; i''m not sure what
the binding semantics of those are, though.

I think Brano''s suggestion of using flock is a better solution.

tom

--
Gin makes a man mean; let''s booze up and riot!


这篇关于__del__模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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