determine if os.system() is done

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    determine if os.system() is done

    suppose i'm calling two system processes, one to unzip, and one to
    “tail” to get the last line. How can i determine when the first
    process is done?

    Example:

    subprocess.Pope n([r"/sw/bin/gzip","-d","access_log. 4.gz"]);

    last_line=subpr ocess.Popen([r"/usr/bin/tail","-n 1","access_log. 4"],
    stdout=subproce ss.PIPE).commun icate()[0]

    of course, i can try workarounds something like os.system("gzip -d
    thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
    determine when a system process is done.

    Xah
    xah@xahlee.org
    ∑ http://xahlee.org/

  • Alessandro Bottoni

    #2
    Re: determine if os.system() is done

    Xah Lee wrote:[color=blue]
    > of course, i can try workarounds something like os.system("gzip -d
    > thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
    > determine when a system process is done.[/color]

    Well, if you use a function of the "popen" family, you get some kind of
    return value from the subprocess on your "output" pipe. You should be able
    to determine if your subprocess has terminated by examining (parsing) this
    output pipe.

    If you use os.system(), you should get a single return value (usually "None"
    or a error code) that you can use for this task.

    In both cases, you may have to use a loop (sleep(x)) to wait for the return
    value (and check it) from within your code.

    Have a look at the docu of the os.process module for details. (Maybe the
    newer "subprocess " module is a better choice...)

    HTH

    -----------------------------------
    Alessandro Bottoni

    Comment

    • Thomas Bellman

      #3
      Re: determine if os.system() is done

      "Xah Lee" <xah@xahlee.org > writes:
      [color=blue]
      > suppose i'm calling two system processes, one to unzip, and one to
      > tail to get the last line. How can i determine when the first
      > process is done?[/color]
      [color=blue]
      > Example:[/color]
      [color=blue]
      > subprocess.Pope n([r"/sw/bin/gzip","-d","access_log. 4.gz"]);[/color]
      [color=blue]
      > last_line=subpr ocess.Popen([r"/usr/bin/tail","-n 1","access_log. 4"],
      > stdout=subproce ss.PIPE).commun icate()[0][/color]
      [color=blue]
      > of course, i can try workarounds something like os.system("gzip -d
      > thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
      > determine when a system process is done.[/color]

      Have you tried reading the manual for the subprocess module? You
      just *might* find the answer to your question if you look at what
      you can do with Popen objects. Actually, just learning about the
      exact semantics of the communicate() method might be enought to
      solve your problem.


      --
      Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
      "I refuse to have a battle of wits with an ! bellman @ lysator.liu.se
      unarmed person." ! Make Love -- Nicht Wahr!

      Comment

      • Fredrik Lundh

        #4
        Re: determine if os.system() is done

        Thomas Bellman wrote:
        [color=blue]
        > Have you tried reading the manual for the subprocess module?[/color]

        han har försökt, men hans tourette tog överhanden:



        </F>



        Comment

        • Jeremy Jones

          #5
          Re: determine if os.system() is done

          Thomas Bellman wrote:
          [color=blue]
          >"Xah Lee" <xah@xahlee.org > writes:
          >
          >
          >[color=green]
          >>suppose i'm calling two system processes, one to unzip, and one to
          >>tail to get the last line. How can i determine when the first
          >>process is done?
          >>
          >>[/color]
          >
          >
          >[color=green]
          >>Example:
          >>
          >>[/color]
          >
          >
          >[color=green]
          >>subprocess.Po pen([r"/sw/bin/gzip","-d","access_log. 4.gz"]);
          >>
          >>[/color]
          >
          >
          >[color=green]
          >>last_line=sub process.Popen([r"/usr/bin/tail","-n 1","access_log. 4"],
          >>stdout=subpro cess.PIPE).comm unicate()[0]
          >>
          >>[/color]
          >
          >
          >[color=green]
          >>of course, i can try workarounds something like os.system("gzip -d
          >>thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
          >>determine when a system process is done.
          >>
          >>[/color]
          >
          >Have you tried reading the manual for the subprocess module? You
          >just *might* find the answer to your question if you look at what
          >you can do with Popen objects.
          >[/color]
          Oh, come on. Don't you know that all Python documentation is rubbish
          and not worth reading, written by IT idiots who throw around useless
          jargon and indulge in extreme forms of self-gratification? Someone of
          the caliber of Xah Lee would *never* stoop so low as to actually read
          the documentation. It is beneath him. Instead, he posts messages to a
          group of IT idiots who throw around useless jargon and indulge in
          extreme forms of self-gratification in posting answers to questions.

          <snip>

          JMJ

          Comment

          • Martin Franklin

            #6
            Re: determine if os.system() is done

            Xah Lee wrote:[color=blue]
            > suppose i'm calling two system processes, one to unzip, and one to
            > “tail” to get the last line. How can i determine when the first
            > process is done?
            >
            > Example:
            >
            > subprocess.Pope n([r"/sw/bin/gzip","-d","access_log. 4.gz"]);
            >
            > last_line=subpr ocess.Popen([r"/usr/bin/tail","-n 1","access_log. 4"],
            > stdout=subproce ss.PIPE).commun icate()[0]
            >
            > of course, i can try workarounds something like os.system("gzip -d
            > thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
            > determine when a system process is done.
            >
            > Xah
            > xah@xahlee.org
            > ∑ http://xahlee.org/
            >[/color]



            I think the idea is you wait for the first call to subprocess.call to
            finish before executing the second...






            call( *args, **kwargs)
            Run command with arguments. *Wait for command to complete*, then
            return the returncode attribute.

            The arguments are the same as for the Popen constructor. Example:

            retcode = call(["ls", "-l"])





            Comment

            • Nainto

              #7
              Re: determine if os.system() is done

              Yeah, I agree. The Python documentation just merey describes what
              arguements a function can take not as much how to use the actual
              function.

              Comment

              • Fredrik Lundh

                #8
                Re: determine if os.system() is done

                "Nainto" <Nainto@gmail.c om> wrote:[color=blue]
                > Yeah, I agree. The Python documentation just merey describes what
                > arguements a function can take not as much how to use the actual
                > function.[/color]

                yeah, that's a really relevant criticism when we're talking about a
                module that contains one function and one class, and for which the
                documentation contains *sixteen* examples.

                </F>



                Comment

                • Lars Gustäbel

                  #9
                  Re: determine if os.system() is done

                  [Fredrik Lundh][color=blue]
                  > han har försökt, men hans tourette tog överhanden:[/color]

                  IMHO it's more likely an Asperger's syndrome.



                  --
                  Lars Gustäbel
                  lars@gustaebel. de

                  Any sufficiently advanced technology is indistinguishab le
                  from magic.
                  (Arthur C. Clarke)

                  Comment

                  • Steve Horsley

                    #10
                    Re: determine if os.system() is done

                    Xah Lee wrote:[color=blue]
                    > suppose i'm calling two system processes, one to unzip, and one to
                    > “tail” to get the last line. How can i determine when the first
                    > process is done?
                    >
                    > Example:
                    >
                    > subprocess.Pope n([r"/sw/bin/gzip","-d","access_log. 4.gz"]);
                    >
                    > last_line=subpr ocess.Popen([r"/usr/bin/tail","-n 1","access_log. 4"],
                    > stdout=subproce ss.PIPE).commun icate()[0]
                    >
                    > of course, i can try workarounds something like os.system("gzip -d
                    > thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
                    > determine when a system process is done.
                    >
                    > Xah
                    > xah@xahlee.org
                    > ∑ http://xahlee.org/
                    >[/color]

                    As far as I can tell from the docs (worth reading),
                    system(command) doesn't return until the process has completed.
                    It would have to do some fancy footwork to return the exit code
                    BEFORE the process had completed!

                    Steve

                    Comment

                    • Martin P. Hellwig

                      #11
                      Re: determine if os.system() is done

                      Lars Gustäbel wrote:[color=blue]
                      > [Fredrik Lundh]
                      >[color=green]
                      >>han har försökt, men hans tourette tog överhanden:[/color]
                      >
                      >
                      > IMHO it's more likely an Asperger's syndrome.
                      >
                      > http://en.wikipedia.org/wiki/Asperger_Syndrome
                      >[/color]

                      I disagree, in his writings I found no evidence of autisme.
                      Actually most of it can be classified as being stubborn against better
                      knowledge, what actually a common thing is.

                      But he does ask the question and by this does admits he is not
                      knowledged in that topic.

                      The only thing I am disappointed at his writing style, most likely he
                      has a disrupted view on social acceptable behavior and communication.
                      These skills might be still in development, so perhaps it is reasonable
                      to give him a chance and wait until he is out of his puberty.

                      --
                      mph

                      Comment

                      • Peter Hansen

                        #12
                        Re: determine if os.system() is done

                        Martin P. Hellwig wrote:[color=blue]
                        > The only thing I am disappointed at his writing style, most likely he
                        > has a disrupted view on social acceptable behavior and communication.
                        > These skills might be still in development, so perhaps it is reasonable
                        > to give him a chance and wait until he is out of his puberty.[/color]

                        He's 37 years old! How long should one be given to mature?

                        -Peter

                        Comment

                        • Xah Lee

                          #13
                          Re: determine if os.system() is done

                          Thanks all.

                          I found the answer, rather easily.
                          To make a system call and wait for it, do:

                          subprocess.Pope n([r"/sw/bin/gzip","-d","access_log. 4.gz"]).wait();

                          ------
                          this post is archived at:


                          Xah
                          xah@xahlee.org
                          ∑ http://xahlee.org/


                          Xah Lee wrote:[color=blue]
                          > suppose i'm calling two system processes, one to unzip, and one to
                          > “tail” to get the last line. How can i determine when thefirst
                          > process is done?
                          >
                          > Example:
                          >
                          > subprocess.Pope n([r"/sw/bin/gzip","-d","access_log. 4.gz"]);
                          >
                          > last_line=subpr ocess.Popen([r"/usr/bin/tail","-n 1","access_log. 4"],
                          > stdout=subproce ss.PIPE).commun icate()[0]
                          >
                          > of course, i can try workarounds something like os.system("gzip -d
                          > thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
                          > determine when a system process is done.
                          >
                          > Xah
                          > xah@xahlee.org
                          > ∑ http://xahlee.org/[/color]

                          Comment

                          • Martin P. Hellwig

                            #14
                            Re: determine if os.system() is done

                            Peter Hansen wrote:[color=blue]
                            > Martin P. Hellwig wrote:
                            >[color=green]
                            >> The only thing I am disappointed at his writing style, most likely he
                            >> has a disrupted view on social acceptable behavior and communication.
                            >> These skills might be still in development, so perhaps it is
                            >> reasonable to give him a chance and wait until he is out of his puberty.[/color]
                            >
                            >
                            > He's 37 years old! How long should one be given to mature?
                            >[/color]
                            Yeah well, eeh I guess that rules out the development part.
                            Although puberty can take a long time (according to my SO) :-)

                            --
                            mph

                            Comment

                            • Martin Franklin

                              #15
                              Re: determine if os.system() is done

                              Xah Lee wrote:[color=blue]
                              > suppose i'm calling two system processes, one to unzip, and one to
                              > “tail” to get the las[/color]
                              t line. How can i determine when the first[color=blue]
                              > process is done?
                              >
                              > Example:
                              >
                              > subprocess.Pope n([r"/sw/bin/gzip","-d","access_log. 4.gz"]);
                              >
                              > last_line=subpr ocess.Popen([r"/usr/bin/tail","-n 1","access_log. 4"],
                              > stdout=subproce ss.PIPE).commun icate()[0]
                              >
                              > of course, i can try workarounds something like os.system("gzip -d
                              > thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
                              > determine when a system process is done.
                              >
                              > Xah
                              > xah@xahlee.org
                              > ∑ http://xahlee.org/
                              >[/color]


                              I know you've found the answer to your question, however for the exact
                              example you gave a much better solution comes to mind...


                              import gzip

                              log_file = gzip.open("acce ss_log.4.gz")
                              last_line = log_file.readli nes()[-1]
                              log_file.close( )


                              Regards
                              Martin





                              Comment

                              Working...