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

Fork You.. Forking and threading..

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

Jul 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2006-07-05, rh0dium <st**********@gmail.comwrote:
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.
Yes they are -- unless you're using a different definition of
the word "backgrounded" than the rest of the Unix world.

What do you mean by "backgrounded"?

What are you trying to accomplish?

--
Grant Edwards grante Yow! Yow!! That's a GOOD
at IDEA!! Eating a whole FIELD
visi.com of COUGH MEDICINE should
make you feel MUCH BETTER!!
Jul 5 '06 #2

P: n/a
In article <11**********************@b68g2000cwa.googlegroups .com>, rh0dium wrote:
if os.fork() == 0:
os.setsid
sys.stdout = open("/dev/null", 'w')
sys.stdin = open("/dev/null", 'r')
I don't know if it's the cause of your problem, but you're not doing
the backgrounding right, it should be:

if os.fork():
os._exit(0)
os.setsid()
os.chdir("/")
fd = os.open("/dev/null", os.O_RDWR)
os.dup2(fd, 0)
os.dup2(fd, 1)
os.dup2(fd, 2)
if fd 2:
os.close(fd)
# do stuff
Jul 5 '06 #3

P: n/a
rh0dium <st**********@gmail.comwrote:
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..
I suspect you want the old fork/setsid/fork trick...

http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

That releases the controlling terminal from your job and it exits
from the process group.

You could probably close / redirect stdin/out/err too. Search for
daemonize.py and you'll find a module which does all this.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 5 '06 #4

P: n/a
Hi Nick!

This is much better than the kludge job I did - Thanks for the help!!
Nick Craig-Wood wrote:
rh0dium <st**********@gmail.comwrote:
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..

I suspect you want the old fork/setsid/fork trick...

http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

That releases the controlling terminal from your job and it exits
from the process group.

You could probably close / redirect stdin/out/err too. Search for
daemonize.py and you'll find a module which does all this.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.