Unexpected exit of Python script

V

vicky

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 ?
 
D

Diez B. Roggisch

vicky said:
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
 
D

Donn

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 ?
Python cleans-up after itself so I would not worry about that until you are an
expert and need to split hairs. Just get on with learning and writing code.

Look at exceptions (try, except) for this kind of thing -- but it's a basic
part of learning Python anyway.

About the only thing I ever found I could not catch was a segfault (under
Linux) and to get around that I open my main app from a smaller one using the
subprocess module. When the main app crashes (which it does sometime) that
process dies suddenly, but it falls-back to the original script which then
detects the error return code and can continue whatever it needs to do.

\d
 
M

Mick Krippendorf

vicky said:
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 :(

You maybe want to use a context manager. Look for 'with statement' and
'contextlib' in your docs.


HTH,
Mick.
 
V

vicky

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.

Thanks for your reply.
/vicky
 
D

Diez B. Roggisch

vicky said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,319
Latest member
LorenFlann

Latest Threads

Top