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. 14 1304
> 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.
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(sel f.lock.fileno() , fcntl.LOCK_EX)
print 'Lock aquired!'
def __del__(self):
fcntl.flock(sel f.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> writes: 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.
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
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!
Tom Anderson wrote: 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
If you bind with self.sock.bind( ('localhost', 4242)) instead, at least
you don't have much of a security risk since the port won't be available
for connections from outside the same machine. Using '' instead of
'localhost' means bind to *all* interfaces, not just the loopback one.
-Peter
On Mon, 15 Aug 2005, Peter Hansen wrote: Tom Anderson wrote:
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 If you bind with self.sock.bind( ('localhost', 4242)) instead, at least you don't have much of a security risk since the port won't be available for connections from outside the same machine.
Excellent suggestion, thanks!
Using '' instead of 'localhost' means bind to *all* interfaces, not just the loopback one.
Doesn't '' mean 'bind to the *default* interface'?
tom
--
All we need now is a little energon and a lotta luck
Tom Anderson wrote: On Mon, 15 Aug 2005, Peter Hansen wrote: Using '' instead of 'localhost' means bind to *all* interfaces, not just the loopback one.
Doesn't '' mean 'bind to the *default* interface'?
What does "default" mean, and is that definition in conflict with what I
said?
The docs say it means INADDR_ANY. They don't say what that means, so
you'd have to read up on the C socket calls to learn more.
Or some helpful soul will clarify for the class... :-)
-Peter
Tom Anderson wrote: 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.
Much worse, it's a bug. That pattern is for programs that need to
respond at a well-known port. In this case it doesn't work; the
fact that *someone* has a certain socket open does not mean that
this particular program is running.
--
--Bryan
Chris Curvey wrote: 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.
For a reasonably portable solution, leave the lock file open.
On most systems, you cannot delete an open file, and if the
program terminates, normally or abnormally, the file will be
closed.
When the program starts, it looks for the lock file, and if
it's there, tries to delete it; if the delete fails, another
instance is probably running. It then tries to create the
lock file, leaving it open; if the create fails, you probably
lost a race with another instance. When exiting cleanly, the
program closes the file and deletes it.
If the program crashes without cleaning up, the file will still
be there, but a new instance can delete it, assuming
permissions are right.
There are neater solutions that are Unix-only or Windows-only.
See BranzoZ's post for a Unix method.
--
--Bryan br************* **********@yaho o.com wrote: For a reasonably portable solution, leave the lock file open. On most systems, you cannot delete an open file,..
On most UNIXes, you can delete an open file.
Even flock-ed. This is BTW also an hack around flock.
1. Process A opens file /var/tmp/test1, and flocks descriptor.
2. Process H unlinks /var/tmp/test1
3. Process B opens file /var/tmp/test1, and flocks _another_
descriptor
4. Processes A and B are running simultaneously
Do you need protection agains H ?
Use file that is writeable by A and B in a directory that is
writeable only by root.
BranoZ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: seth |
last post by:
Last week I encountered an AttributeError in my unit tests that I
wasn'table to catch with an "except AttributeError" statement.
The problem stemmed from a class that raised an error inside
__init__and defined a __del__ method to clean up resources. I then
discovered asimilar problem in the shelve module. This led me to two
importantdiscoveries:
1. Attributes defined in __init__ after an error is raised will not be
apart of the...
|
by: Kepes Krisztian |
last post by:
Hi !
I very wonder, when I get exp. in java with GC.
I'm Delphi programmer, so I get used to destructorin objects.
In Java the final method is not same, but is like to destructor (I has
been think...).
And then I try with some examples, I see, that the Java GC is
|
by: Peter Abel |
last post by:
I have an application, which is an instance of a class
with a deeply nested object hierarchy. Among others one
method will be executed as a thread, which can be stopped.
Everything works fine except that when deleting the main
instance - after the thread has been stopped -
the __del__ method will not be carried out.
Tough a simple example works as expected:
>>> class A:
.... def __init__(self):
|
by: Emmanuel |
last post by:
Hi,
I run across this problem, and couldn't find any solution (python 2.2.2)
:
Code :
===========
from __future__ import generators
>>> class titi:
|
by: schwerdy |
last post by:
Hello developers!
I'm using Python 2.3.4 under debian Sarge and want to write a small
logger class. My source code reads:
#***************************************************
import sys, time
from fcntl import *
class Log(object):
"""
| |
by: Mike C. Fletcher |
last post by:
I'm looking at rewriting parts of Twisted and TwistedSNMP to eliminate
__del__ methods (and the memory leaks they create). Looking at the docs
for 2.3's weakref.ref, there's no mention of whether the callbacks are
held with a strong reference. My experiments suggest they are not...
i.e. I'm trying to use this pattern:
class Closer( object ):
"""Close the OIDStore (without a __del__)"""
def __init__( self, btree ):
"""Initialise the...
|
by: Baoqiu Cui |
last post by:
Today I was playing with a small Python program using Python 2.4
on Cygwin (up-to-date version, on Windows XP), but ran into a
strange error on the following small program (named bug.py):
-------------------------------
#!/usr/bin/python
class Person:
population = 0
def __del__(self):
|
by: Erwan Adam |
last post by:
Hello all,
Can someone reproduce this bug ... I use :
python
Python 2.4.3 (#2, Sep 18 2006, 21:07:35)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
First test :
|
by: George Sakkis |
last post by:
I'm baffled with a situation that involves:
1) an instance of some class that defines __del__,
2) a thread which is created, started and referenced by that instance,
and
3) a weakref proxy to the instance that is passed to the thread
instead of 'self', to prevent a cyclic reference.
This probably sounds like gibberish so here's a simplified example:
==========================================
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |