Cleaning up after C module

J

j1k1cki

Hello,

I have a Python module written in C that spawns and kills processes
using OS-specific mechanisms. I want to kill all spawned processes when
the interpreter exits. I tried to wrap every spawned process in a
Python object like this:

import cmodule
class Process:
__init__(self, execname): self.pid = cmodule.spawn(execname)
__del__(self): cmodule.kill(self.pid)

p = Process("foo")

but this does not work, I am getting and exception inidicating that
'cmodule' is 'None' in '__del__()'.

Moreover, the Language Reference states that "It is not guaranteed that
__del__() methods are called for objects that still exist when the
interpreter exits", so it looks like this approach is wrong anyway. How
do I do this right?

Thanks very much in advance
Grzegorz

PS: Python 2.3.3
 
F

Fredrik Lundh

I have a Python module written in C that spawns and kills processes
using OS-specific mechanisms. I want to kill all spawned processes when
the interpreter exits. I tried to wrap every spawned process in a
Python object like this:

import cmodule
class Process:
__init__(self, execname): self.pid = cmodule.spawn(execname)
__del__(self): cmodule.kill(self.pid)

p = Process("foo")

but this does not work, I am getting and exception inidicating that
'cmodule' is 'None' in '__del__()'.

module-level names may be cleaned away before your objects, so if you want
to make sure you can reach a module-level object, create your own binding:

def__del__(self, cmodule=cmodule): cmodule.kill(self.pid)
Moreover, the Language Reference states that "It is not guaranteed that
__del__() methods are called for objects that still exist when the
interpreter exits", so it looks like this approach is wrong anyway. How
do I do this right?

the "atexit" module might be what you need.

</F>
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top