Execute code after death of all child processes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Franz

    #1

    Execute code after death of all child processes

    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
  • Mike Meyer

    #2
    Re: Execute code after death of all child processes

    mf1987@arcor.de (Markus Franz) writes:
    [color=blue]
    > 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.[/color]

    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 <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • Terry Reedy

      #3
      Re: Execute code after death of all child processes


      "Markus Franz" <mf1987@arcor.d e> wrote in message
      news:576fb15a.0 412240314.13b6c a6f@posting.goo gle.com...

      I am a little puzzled about your variable declaration and use:
      [color=blue]
      > import sys, os, time
      > texts = ['this is text1', 'this is text 2'][/color]

      You don't seem to use 'texts',
      [color=blue]
      > for current_text in env_sources[0:]:[/color]

      or define 'env_sources',
      [color=blue]
      > pid = os.fork()
      > if pid == 0:
      > time.sleep(2)
      > print current_text
      > break[/color]

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

      Terry J. Reedy



      Comment

      Working...