Cleaning the current environment after a fork

R

Robin Haswell

Hey guys

I want to fork a process, but my scope has lots of stuff that the child
won't need. Is it possible to clean the current environment of cruft so it
is collected and freed? Basically I want it to go something like this.
This is my first forking Python app, by the way:

# {{{ My app

import lots, of, stuff

# ...snip...

ret = os.fork()

if ret is not 0:

# clean my environment of lots, of, stuff so they are collected and
# memory is freed

import mymodule
worker = mymodule.MyClass(**kw) # some arguments that I have worked out
worker.Go()
sys.exit()._exit()

# carry on

# }}}

Obviously I only want to selectively clean the environment. I was thinking
something along the lines of:

tokeep = ["kw", "others"]
for obj in dir():
if obj not in tokeep:
delattr(?, obj)

However I can't access the local scope with (del|get|set|has)attr.. can I?

I would like to do it this way instead of os.popen() because there are
structures I'd like my child to have which would be tricky to pass through
shell args.

Any help would be appreciated :)

Thanks

-Rob
 
D

Diez B. Roggisch

I want to fork a process, but my scope has lots of stuff that the child
won't need. Is it possible to clean the current environment of cruft so it
is collected and freed? Basically I want it to go something like this.
This is my first forking Python app, by the way:

I'm not an expert of this - but if all the reason you want to do this are
memory concerns, AFAIK forking will only make a copy of those memory-pages
that are actually written to by the forked process - for efficiency
reasons.

And even if that wasn't the case: I think as long as you don't run into
memory-troubles, don't do it. Its complex, flaky and thus an unnecessary
source of failure.

Diez
 
R

Robin Haswell

And even if that wasn't the case: I think as long as you don't run into
memory-troubles, don't do it. Its complex, flaky and thus an unnecessary
source of failure.

Yeah that sounds fair enough. Actually I ran in to threading problems so
I sorted out a way of transferring my data

Thanks

-Rob
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top