__del__ pattern?
Tom Anderson
twic at urchin.earth.li
Mon Aug 15 12:11:43 EDT 2005
On Mon, 15 Aug 2005, Chris Curvey wrote:
> Is there a better pattern to follow than using a __del__ method? I just
> need to be absolutely, positively sure of two things:
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!
More information about the Python-list
mailing list