Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Fork You.. Forking and threading..

Reply
Thread Tools

Fork You.. Forking and threading..

 
 
rh0dium
Guest
Posts: n/a
 
      07-05-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.

 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      07-05-2006
On 2006-07-05, rh0dium <(E-Mail Removed)> wrote:
> 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!!
 
Reply With Quote
 
 
 
 
Jon Ribbens
Guest
Posts: n/a
 
      07-05-2006
In article <(E-Mail Removed) .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
 
Reply With Quote
 
Nick Craig-Wood
Guest
Posts: n/a
 
      07-05-2006
rh0dium <(E-Mail Removed)> wrote:
> 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 <(E-Mail Removed)> -- http://www.craig-wood.com/nick
 
Reply With Quote
 
rh0dium
Guest
Posts: n/a
 
      07-06-2006
Hi Nick!

This is much better than the kludge job I did - Thanks for the help!!


Nick Craig-Wood wrote:
> rh0dium <(E-Mail Removed)> wrote:
> > 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 <(E-Mail Removed)> -- http://www.craig-wood.com/nick


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vonage and Forking 1506 UK VOIP 0 02-01-2011 09:31 AM
os.fork and pty.fork Eric Snow Python 0 01-08-2009 06:32 AM
threading/forking and IPC Sophia Cao Python 2 10-14-2005 11:01 PM
forking and using MySQL James Perl 1 05-19-2004 12:41 PM
Python API, objects, globals, and forking. David M. Wilson Python 0 04-05-2004 03:11 AM



Advertisments