Unexpected exit of Python script

Diez B. Roggisch deets at nospam.web.de
Wed Oct 14 10:02:24 EDT 2009


vicky wrote:

> On Oct 14, 5:52 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> vicky wrote:
>> > Hello All,
>>
>> > I am a new Python user and don't know much about it.
>>
>> > I just want to know that, due to any reason if a script exits, is
>> > their some way to release all the resources acquired by the script
>> > during execution ?
>>
>> > Actually In my system I want to execute some piece of code at the time
>> > of script exit (expected or unexpected) to ensure the release of all
>> > the resources. I don't know how to do that :(
>>
>> > Can anyone please help me ?
>>
>> What resources? Usually, the garbage-collection and the OS will do that
>> for you anyway. So are there concrete problems you observe?
>>
>> Also, take a look at the atexit-module.
>>
>> Diez
> 
> According to the documentation of atexit module:
> 
> The atexit module defines a single function to register cleanup
> functions. Functions thus registered are automatically executed upon
> normal interpreter termination.
> 
> Note: the functions registered via this module are not called when the
> program is killed by a signal, when a Python fatal internal error is
> detected, or when os._exit() is called.
> 
> Actually I have Python ported on vx-works. During initialization I am
> initializing some python interpreters, and each interpreter is
> associated with a specific buffer to send or receive the messages
> using underlying protocol (embedded in C library). Using same library
> I am using the subscription functionality. So when I am using Python
> to made subscriptions it register an entry. But if some user forgets
> to clear the subscription or script exited accidently due to some
> error, subscription still remain active. And when next time the same
> interpreter is used to execute some script, the older subscription
> create a trouble for the user. I want some implementation which
> guarantees the clearance of all the subscriptions at script exit
> (expected or unexpected) automatically.

Ok, the embedded part is crucial here - because the atexit is AFAIK
implemented in terms of the "real" atexit - so it will only be called when
the process dies.

In your case, I guess you have two options: 

 - make the interpreter execute scripts that always have try/finally with
some cleanup-code in the finally

 - clean up after the interpreter in C++

Diez



More information about the Python-list mailing list