Execute code after death of all child processes

Discussion in 'Python' started by Markus Franz, Dec 24, 2004.

  1. Markus Franz

    Markus Franz Guest

    Hallo!


    I have a problem with this little script written in Python:


    import sys, os, time
    texts = ['this is text1', 'this is text 2']
    for current_text in env_sources[0:]:
    pid = os.fork()
    if pid == 0:
    time.sleep(2)
    print current_text
    break

    As you is create some child processes depending on the number of vars
    in ?texts?. My problem is: How can I run some code after all child
    processes have been finished? (The code should be run only in the
    parent process.)

    I tried:

    import sys, os, time
    texts = ['this is text1', 'this is text 2']
    for current_text in env_sources[0:]:
    pid = os.fork()
    if pid == 0:
    time.sleep(2)
    print current_text
    break
    os.waitpid(-1, 0)
    print 'this is the end'

    But this didn't work, 'this is the end' will be showed several times.

    Can you help me?

    Tanks.


    Markus
     
    Markus Franz, Dec 24, 2004
    #1
    1. Advertising

  2. Markus Franz

    Mike Meyer Guest

    (Markus Franz) writes:

    > Hallo!
    >
    >
    > I have a problem with this little script written in Python:
    >
    >
    > import sys, os, time
    > texts = ['this is text1', 'this is text 2']
    > for current_text in env_sources[0:]:
    > pid = os.fork()
    > if pid == 0:
    > time.sleep(2)
    > print current_text
    > break
    >
    > As you is create some child processes depending on the number of vars
    > in ?texts?. My problem is: How can I run some code after all child
    > processes have been finished? (The code should be run only in the
    > parent process.)
    >
    > I tried:
    >
    > import sys, os, time
    > texts = ['this is text1', 'this is text 2']
    > for current_text in env_sources[0:]:
    > pid = os.fork()
    > if pid == 0:
    > time.sleep(2)
    > print current_text
    > break
    > os.waitpid(-1, 0)
    > print 'this is the end'
    >
    > But this didn't work, 'this is the end' will be showed several times.


    import sys, os, time
    texts = ['this is text1', 'this is text2']
    for current_text in env_sources[0:]:
    pid = os.fork()
    if pid == 0:
    time.sleep(2)
    print current_text
    break
    if pid != 0:
    os.waitpid(-1, 0)
    print 'this is the end'

    Basically, make sure that none of the children run the waitpid and
    last print.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Dec 24, 2004
    #2
    1. Advertising

  3. Markus Franz

    Terry Reedy Guest

    "Markus Franz" <> wrote in message
    news:...

    I am a little puzzled about your variable declaration and use:

    > import sys, os, time
    > texts = ['this is text1', 'this is text 2']


    You don't seem to use 'texts',

    > for current_text in env_sources[0:]:


    or define 'env_sources',

    > pid = os.fork()
    > if pid == 0:
    > time.sleep(2)
    > print current_text
    > break


    so I don't see how this could run (even with indents inserted back). What
    am I missing?

    Terry J. Reedy
     
    Terry Reedy, Dec 24, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,183
    David Schwartz
    Dec 9, 2003
  2. Markus Franz
    Replies:
    2
    Views:
    365
    Denis S. Otkidach
    Dec 28, 2004
  3. Replies:
    2
    Views:
    254
    Joel VanderWerf
    Nov 10, 2006
  4. Jan Christoph
    Replies:
    1
    Views:
    172
    Jan Christoph
    Oct 4, 2007
  5. asim malik
    Replies:
    1
    Views:
    251
    Martijn Lievaart
    Sep 13, 2009
Loading...

Share This Page