Cleaning the current environment after a fork

Robin Haswell rob at digital-crocus.com
Thu Apr 6 06:35:38 EDT 2006


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



More information about the Python-list mailing list