[Tutor] python's bash wait and ampersand equivalent?

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Tue Apr 26 09:34:24 CEST 2005


I do not understand why you don't want the so simple fork/exec pattern !
In UNIX programming this is the way to go ...

I cannot think of anything simpler than that :

for i in xrange( 10 ):
   pid = os.fork()
   if not pid:
     os.execv( "/bin/echo", [ "echo", "toto" ] )
try:
   while True:
     os.wait()
except OSError:
   pass
print "all done"

I fear that python programming is a little less process-oriented than 
shells ... but then, it is just normal for an all-purpose language (or 
at least an all-purpose wanna-be language ;) ).

Pierre

chumpy town a écrit :
> Hello all,
> I am trying to convert from bash to python for scripting.  What is the
> simplest & cleanest way to do the following in python:
> 
> #!/bin/bash
> for i in `seq 1 1000`
>   do
>   my-other-script &
> done
> wait
> echo "all done"
> 
> I at first tried os.system("my-other-script &") and os.wait() but this
> caused os.wait() to throw an exception and say no child processes. 
> Alternatively, I know I can use os.fork(), os.exec() and os.wait() but
> this seems cumbersome.  As a second alternative, os.spawnlp() and
> os.wait() works, but I need to additionally redirect the spawned
> process' stdout to the spawnee's stdout, etc. - again an extra step.
> 
> Experienced python programmers, what's the best thing to do here?  Thanks a lot!
> 
> -david
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Tutor mailing list