By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

Cleaning up after C module

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<j1*****@hotmail.com> wrote:
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>

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.