Execute code after death of all child processes - (corrected posting)

Discussion in 'Python' started by Markus Franz, Dec 25, 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 texts[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 25, 2004
    #1
    1. Advertising

  2. Markus Franz

    Jeff Epler Guest

    Re: Execute code after death of all child processes - (correctedposting)

    First, you'll want to exit from each forked copy, or else it will reach
    the code-after-the-for-loop:
    import sys, os, time
    texts = ['this is text1', 'this is text 2']
    for current_text in texts[0:]:
    pid = os.fork()
    if pid == 0:
    time.sleep(2)
    print current_text
    raise SystemExit

    Next, you'll want to wait for each process you started:
    for current_text in texts:
    os.waitpid(-1, 0)
    print 'this is the end'


    $ python /tmp/franz.py
    this is text1
    this is text 2
    this is the end

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.6 (GNU/Linux)

    iD8DBQFBzXcmJd01MZaTXX0RAjtXAKCV0GZ7o3GW3bJ0pBRdgH+CyLcjAgCeJ2cO
    Op+TNBqmhANGSEhIUYAq6eE=
    =O4tl
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Dec 25, 2004
    #2
    1. Advertising

  3. Re: Execute code after death of all child processes - (correctedposting)

    On Sat, 25 Dec 2004 08:20:24 -0600
    Jeff Epler <> wrote:

    > Next, you'll want to wait for each process you started:
    > for current_text in texts:
    > os.waitpid(-1, 0)
    > print 'this is the end'


    This can eventually die with exception. The proper way is:

    try:
    while True:
    os.waitpid(-1, 0)
    except OSError, exc:
    if exc.errno!=errno.ECHILD:
    raise

    --
    Denis S. Otkidach
    http://www.python.ru/ [ru]
     
    Denis S. Otkidach, Dec 28, 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,134
    David Schwartz
    Dec 9, 2003
  2. Markus Franz
    Replies:
    2
    Views:
    274
    Terry Reedy
    Dec 24, 2004
  3. Replies:
    2
    Views:
    247
    Joel VanderWerf
    Nov 10, 2006
  4. Jan Christoph
    Replies:
    1
    Views:
    166
    Jan Christoph
    Oct 4, 2007
  5. asim malik
    Replies:
    1
    Views:
    237
    Martijn Lievaart
    Sep 13, 2009
Loading...

Share This Page