Fork You.. Forking and threading..

rh0dium steven.klass at gmail.com
Wed Jul 5 13:21:37 EDT 2006


Hi all,

I have a problem with putting a job in the background. Here is my
(ugly) script which I am having problems getting to background.  There
are threads about doing

     python script.py &

and others

     nohup python script.py &

and yet others

     ( python script.py > /dev/null & ) &

Regardless timing these all produce (roughly the same time) and none of
them are backgrounded..  So I attempted to first thread my little
script ( which I think is still needed for optimization purposes ) and
then I forked it.  But it still does not background it..  Here is what
I have..

--------------------------------------------------------------------------------

# File   pushsync.py
#
#

import logging,traceback,os,sys,paramiko,threading
from RunSSHCmd import RunSSHCmd

# Set's up a basic logger
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s
%(levelname)-8s %(message)s",
                    datefmt='%d %b %Y %H:%M:%S', stream=sys.stderr)

# This little mess identifies (hopefully) the current module name
try:
    module=
os.path.basename(traceback.extract_stack(limit=2)[1][0]).split(".py")[0]+"."
except:
    module =
os.path.basename(traceback.extract_stack(limit=2)[0][0]).split(".py")[0]+"."

# Class declarations
class syncer(threading.Thread):

    def __init__(self,host,keyfile,passwd):
        threading.Thread.__init__(self)
        self.log = logging.getLogger(module+self.__class__.__name__)
        self.keyfile = keyfile
        self.passwd = passwd
        self.host= host

    def run(self):
        # Import the key..
        key = paramiko.DSSKey.from_private_key_file(self.keyfile,
password=self.passwd
        agent_keys=[key]

        print "Updating host %s" % self.host
        results = RunSSHCmd( host=self.host, cmd="p4 sync", timeout=10,
keys=agent_keys ).run()
        if results is None:
            log.error("We had a problem..")
        return results


# OK let's get busy
def main(hosts,keyfile, passwd):

    # Fork You!
    #
    if os.fork() == 0:
        os.setsid
        sys.stdout = open("/dev/null", 'w')
        sys.stdin = open("/dev/null", 'r')

        log = logging.getLogger(module+sys._getframe().f_code.co_name )

        for host in hosts:
            log.info("Updating host %s" % host)
            syncer(host,keyfile,passwd).start()

# General run..
if __name__ == '__main__':

    # SSH Keyfile
    KEYFILE=os.environ['HOME'] + '/.ssh/id_dsa'
    PASSWD='YXV0MG1hdDM=\n'

    # Perforce writable hosts
    HOSTS="savoy", "phxlfs03"

    main(HOSTS,KEYFILE,PASSWD)


--------------------------------------------------------------------------------

Now when I run this from the command line it appears to work.  But when
I call it from my other app (perforce) it does not background it.  I
can tell because I expect the command return immediately ( because of
the fork) but it doesn't and seems to take a very long time..

Can someone who's very familiar with this help me out please.




More information about the Python-list mailing list