473,699 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reliable destruction

Hello,

I have a class measurement representing a physical measurement.
Different objects in this class represent laboratory equipment, which
might raise an exception (e.g. overtemperature ).

In any case the equipment has to be switched off after the experiment,
since if a
power supply stays in the on state for a prolonged time equipment may
be
destroyed. Switching off is done by invoking the destructors of the
instruments.

My measurement looks like this:

class measurement:
def __init__(self):
self.setup()
self.run()

def setup(self):
self.powerSuppl y=apparate.Powe rSupply()
self.magnet=app arate.magnet() # Exception("Comm unication Error")
self.thermo=app arate.thermomet er()
# some 5 more instruments

def run():
for i in range(100)
self.powerSuppl y.setCurrent(i) # Exception("Over current")
self.magnet.set Field(0.5*i)
Different measurements are executed in a script which might run
overnight:
If one measurement raises an exception the next one might still work
and I don't
want to loose the results from the following experiments.

try:
measurement()
except:
pass
try:
measurement2()
except:
pass
An exception might be thrown anywhere in init or run if e.g. the
PowerSupply
overheats. Maybe an asynchronous event might happen, too (user
interrupt with ^C but I might live without that if it is impossible to
handle)

My questions are:
1) under normal conditions (no exceptions) is there a guarantee, that
__del__ of
all instruments is called at the end of measurement()?

2) if an exception is thrown, will all instruments be deleted if the
error
occurs in run() ?
(only the instruments already initialized if the error occurs
in setup() )?

I am using CPython (on WinXP) and there are no reference cycles between
the instruments.

I have to stress again that a reliable finalization is important and
cannot wait
until the interpreter shuts down.

I have tried this and it seems to work but this is of course no
guarantee.

Many thanks

Aug 4 '05 #1
5 1339
Pi************* ***@uni-konstanz.de wrote:
Hello,

I have a class measurement representing a physical measurement.
Different objects in this class represent laboratory equipment, which
might raise an exception (e.g. overtemperature ).

In any case the equipment has to be switched off after the experiment,
since if a
power supply stays in the on state for a prolonged time equipment may
be
destroyed. Switching off is done by invoking the destructors of the
instruments.

My measurement looks like this:

class measurement:
def __init__(self):
self.setup()
self.run()

def setup(self):
self.powerSuppl y=apparate.Powe rSupply()
self.magnet=app arate.magnet() # Exception("Comm unication Error")
self.thermo=app arate.thermomet er()
# some 5 more instruments

def run():
for i in range(100)
self.powerSuppl y.setCurrent(i) # Exception("Over current")
self.magnet.set Field(0.5*i)
Different measurements are executed in a script which might run
overnight:
If one measurement raises an exception the next one might still work
and I don't
want to loose the results from the following experiments.

try:
measurement()
except:
pass
try:
measurement2()
except:
pass
An exception might be thrown anywhere in init or run if e.g. the
PowerSupply
overheats. Maybe an asynchronous event might happen, too (user
interrupt with ^C but I might live without that if it is impossible to
handle)

My questions are:
1) under normal conditions (no exceptions) is there a guarantee, that
__del__ of
all instruments is called at the end of measurement()?

2) if an exception is thrown, will all instruments be deleted if the
error
occurs in run() ?
(only the instruments already initialized if the error occurs
in setup() )?

I am using CPython (on WinXP) and there are no reference cycles between
the instruments.

I have to stress again that a reliable finalization is important and
cannot wait
until the interpreter shuts down.

I have tried this and it seems to work but this is of course no
guarantee.


I would suggest an explicit tearDown() method

try:
m = measurement()
try:
m.setup()
m.run()
finally:
m.tearDown()
except KeyboardInterru pt:
# user pressed ctrl-C
print "***BREAK"
sys.exit(1)
except:
# you should at least log the exception for later debugging
traceback.print _exc()

and remove the calls to setup() and run() from the constructor.

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Aug 4 '05 #2
Hello Benjamin,

What would happen if an exception was thrown in the middle of setup()?
tearDown could not handle this case without having a list of the
objects already constructed (Or I would have to rely on the automatic
call to __del__, if it is reliable).
There is still some problem:
Imagine a communication error in run() which would cause del to fail on
the instrument.
Anyway, I think this case is still more difficult to handle.

Aug 4 '05 #3
Pi************* ***@uni-konstanz.de wrote:
Hello Benjamin,

What would happen if an exception was thrown in the middle of setup()?
tearDown could not handle this case without having a list of the
objects already constructed (Or I would have to rely on the automatic
call to __del__, if it is reliable).
class measurement:
def __init__(self):
self.powerSuppl y = None
...

def setup(self):
self.powerSuppl y=apparate.Powe rSupply()
...

def tearDown(self):
if self.powerSuppl y is not None:
try:
self.powerSuppl y.tearDown()
except:
# Exception in powerSupply.tea rDown() should not stop
# the following tearDown()s from being executed
traceback.print _exc()

...

There is still some problem:
Imagine a communication error in run() which would cause del to fail on
the instrument.
Not really sure, if I understand what you mean? Does my tearDown() above
covers this?
Anyway, I think this case is still more difficult to handle.


Reliable, fail-safe software *is* hard to design and implement, that's for
sure..
Be happy that it's just a power supply that could overheat and not the core
of a nuclear power plant.

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Aug 4 '05 #4
[Pi************* ***@uni-konstanz.de]
My questions are:
1) under normal conditions (no exceptions) is there a guarantee, that
__del__ of
all instruments is called at the end of measurement()?

2) if an exception is thrown, will all instruments be deleted if the
error
occurs in run() ?
(only the instruments already initialized if the error occurs
in setup() )?

I am using CPython (on WinXP) and there are no reference cycles between
the instruments.

I have to stress again that a reliable finalization is important and
cannot wait
until the interpreter shuts down.

I have tried this and it seems to work but this is of course no
guarantee.


On the plus side, Python does guarantee destruction in the absence of
cycles when the last reference disappears. If there is a cycle, you
have to wait for GC. If you can't wait, then schedule a gc.collect()
to run periodically.

On the minus side, this is a somewhat brittle and error-prone design
strategy. A single, accidental persistent reference is sufficient to
cause failure -- that is a land-mine for all future maintainers of your
code. It is better to make explicit tear-down calls and to wrap
finalization is a try/finally suite.

The simplified code in your post suggests that the instrument shut-off
can be placed at the end of the run() method -- loosely translated as
turn-off the lights when you're done.

An alternative strategy is to periodically poll resources and shut them
off if they are not in use -- loosely translated as having a security
guard turn-off any coffee-pots that were left on by frazzled
programmers as they leave at odd hours of the night.
Raymond

Aug 4 '05 #5
Hello,

your idea sounds good and handles the exception on teardown as well.
(I did not think about the if XXX!=None check in teardown())
I will now provide each of the instruments with an explicit shutdown()
method which frees the interface card as well.

These methods will be called in a finally clause after run.
I think this is much better than to wait for or rely on garbage
collection.

Of course there is a hardware protection of the setup as well.

I am amazed how well Python handles lab instrumentation , it is really
fun to develop with Py.
Many thanks for your help

Aug 5 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1858
by: Gandalf | last post by:
Hello (it's the newbie again). If I have a class class Foo{ public: Foo(){cout<<"Making"<<endl;} ~Foo(){cout<<"Destroying"<<endl;} }; void func(Foo x){}
0
1230
by: Stephan Keil | last post by:
Hi all, consider a class class X { static X& OneX() { static X sgl1; return sgl1; } static X& TheX() { static X sgl2(OneX()); return sgl2; } }; and two source files with global variables
0
1553
by: relisoft | last post by:
Seattle, WA -- Seattle-based Reliable Software® announces the release their Windows Library into the public domain. Reliable Software Windows Library, RSWL, is the foundation for their compact, feature rich applications including the flagship product, Code Co-op. The use of RSWL in both individual and commercial products is free and not restricted to public domain projects. RSWL provides a thin, easy to understand, object-oriented layer...
9
2998
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. For those that have no idea what I am talking about (I learn a lot reading posts on subjects in which I am clueless), consider the following code snippet: for line in file(name): ...print line,
11
1756
by: OlafMeding | last post by:
Because of multithreading semantics, this is not reliable. This sentence is found in the Python documentation for "7.8.1 Queue Objects". This scares me! Why would Queue.qsize(), Queue.empty( ), and a Queue.full() not be reliable? Looking at the source code of Queue.py, all 3 calls use a mutex (based on thread.allocate_lock()). Does this mean that the thread.allocate_lock() mechanism is not reliable (scary indeed) or does
1
2184
by: relisoft | last post by:
SEATTLE, Washington. - July 12, 2006: Reliable Software® announces the upcoming release of Code Co-op® version 5.0. Code Co-op is an affordable peer-to-peer version control system for distributed development enabling collaboration through Email, LAN, or VPN-no server required. The upcoming release of Code Co-op 5.0 is due out this fall. With this release Reliable Software continues its track record of innovation by introducing...
6
2251
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes contains a pointer to int values as a filed. In the definition (implementation) of constructor I use this pointer to create table of int values with the new operator. The number of elements of the table is provided by the user during execution of the...
2
4312
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
7
3408
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case class is derived as virtual? How does the order of construction/destruction is impacted if the base class is derived as virtual or non virtual? just see the example below.
0
8685
marktang
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...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8905
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,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.