Problems with os.spawnv

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henrik Lied

    #1

    Problems with os.spawnv

    Hi there!

    I'm trying to convert a video in a background process.
    The scenario I'm after:
    1. The user uploads a video
    2. The video is saved in my media directory, and the database is
    populated with the references
    3. The video gets converted to FLV - but the user shouldn't have to
    wait around for this to happen

    So, I thought to myself that spawnv would be a good fit for this. The
    problem is that it doesn't fire of the command.

    Here's my test script: http://dpaste.com/hold/7981/

    Why won't this work? The while-loop is printed, but the os command
    isn't executed. I've also tried to specify the whole path to mencoder
    (/opt/local/bin/mencoder), but to no use.

  • kyosohma@gmail.com

    #2
    Re: Problems with os.spawnv

    On Apr 5, 4:19 pm, "Henrik Lied" <henrikl...@gma il.comwrote:
    Hi there!
    >
    I'm trying to convert a video in a background process.
    The scenario I'm after:
    1. The user uploads a video
    2. The video is saved in my media directory, and the database is
    populated with the references
    3. The video gets converted to FLV - but the user shouldn't have to
    wait around for this to happen
    >
    So, I thought to myself that spawnv would be a good fit for this. The
    problem is that it doesn't fire of the command.
    >
    Here's my test script:http://dpaste.com/hold/7981/
    >
    Why won't this work? The while-loop is printed, but the os command
    isn't executed. I've also tried to specify the whole path to mencoder
    (/opt/local/bin/mencoder), but to no use.
    I don't know what the deal is. Maybe you should try the subprocess
    module since it replaces the os.spawn* modules and do a
    subprocess.Pope n instead? Or don't assign the result to a variable
    since you told it not to wait. You may need to do a combination of the
    subprocess module and one of the Threading modules.

    Mike

    Comment

    • Henrik Lied

      #3
      Re: Problems with os.spawnv

      On Apr 5, 11:33 pm, kyoso...@gmail. com wrote:
      On Apr 5, 4:19 pm, "Henrik Lied" <henrikl...@gma il.comwrote:
      >
      >
      >
      Hi there!
      >
      I'm trying to convert a video in a background process.
      The scenario I'm after:
      1. The user uploads a video
      2. The video is saved in my media directory, and the database is
      populated with the references
      3. The video gets converted to FLV - but the user shouldn't have to
      wait around for this to happen
      >
      So, I thought to myself that spawnv would be a good fit for this. The
      problem is that it doesn't fire of the command.
      >
      Here's my test script:http://dpaste.com/hold/7981/
      >
      Why won't this work? The while-loop is printed, but the os command
      isn't executed. I've also tried to specify the whole path to mencoder
      (/opt/local/bin/mencoder), but to no use.
      >
      I don't know what the deal is. Maybe you should try the subprocess
      module since it replaces the os.spawn* modules and do a
      subprocess.Pope n instead? Or don't assign the result to a variable
      since you told it not to wait. You may need to do a combination of the
      subprocess module and one of the Threading modules.
      >
      Mike
      Thanks for the quick reply, Mike!

      I've taken a look at the Subprocess module, but it's all a bit new to
      me, if you know what I mean.
      You wouldn't be able to supply me with an example, would you?

      Thanks!

      Comment

      • Gabriel Genellina

        #4
        Re: Problems with os.spawnv

        En Thu, 05 Apr 2007 18:19:56 -0300, Henrik Lied <henriklied@gma il.com>
        escribió:
        So, I thought to myself that spawnv would be a good fit for this. The
        problem is that it doesn't fire of the command.
        >
        Here's my test script: http://dpaste.com/hold/7981/
        >
        Why won't this work? The while-loop is printed, but the os command
        isn't executed. I've also tried to specify the whole path to mencoder
        (/opt/local/bin/mencoder), but to no use.
        >
        You are using:
        v = os.spawnv(os.P_ NOWAIT, "mencoder", "/Users/henriklied/test.mov
        -ofps 25 -o test.flv ...")

        Read the docs about the spawnv function:


        In particular "...The "v" variants are good when the number of parameters
        is variable, with the arguments being passed in a list or tuple as the
        args parameter. In either case, the arguments to the child process must
        start with the name of the command being run."

        So, for spawnv, you should build a list with each argument as an item,
        being "mencoder" the first item.
        But in your case it's a lot easier to use spawnl:

        v = os.spawnl(os.P_ NOWAIT, "mencoder", "mencoder",
        "/Users/henriklied/test.mov", "-ofps", "25", "-o", "...")

        --
        Gabriel Genellina

        Comment

        • Henrik Lied

          #5
          Re: Problems with os.spawnv

          On Apr 5, 11:39 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
          wrote:
          En Thu, 05 Apr 2007 18:19:56 -0300, Henrik Lied <henrikl...@gma il.com>
          escribió:
          >
          So, I thought to myself that spawnv would be a good fit for this. The
          problem is that it doesn't fire of the command.
          >
          Here's my test script:http://dpaste.com/hold/7981/
          >
          Why won't this work? The while-loop is printed, but the os command
          isn't executed. I've also tried to specify the whole path to mencoder
          (/opt/local/bin/mencoder), but to no use.
          >
          You are using:
          v = os.spawnv(os.P_ NOWAIT, "mencoder", "/Users/henriklied/test.mov
          -ofps 25 -o test.flv ...")
          >
          Read the docs about the spawnv function: http://docs.python.org/lib/os-process.html#l2h-2749
          >
          In particular "...The "v" variants are good when the number of parameters
          is variable, with the arguments being passed in a list or tuple as the
          args parameter. In either case, the arguments to the child process must
          start with the name of the command being run."
          >
          So, for spawnv, you should build a list with each argument as an item,
          being "mencoder" the first item.
          But in your case it's a lot easier to use spawnl:
          >
          v = os.spawnl(os.P_ NOWAIT, "mencoder", "mencoder",
          "/Users/henriklied/test.mov", "-ofps", "25", "-o", "...")
          >
          --
          Gabriel Genellina
          Hi Gabriel,

          Thanks for your reply - but I'm afraid to tell you that spawnl didn't
          do the trick either.
          Here's the full command I used: http://dpaste.com/hold/7982/

          I'd still love to get a working example of my problem using the
          Subprocess module. :-)

          Comment

          • Gabriel Genellina

            #6
            Re: Problems with os.spawnv

            En Thu, 05 Apr 2007 18:53:04 -0300, Henrik Lied <henriklied@gma il.com>
            escribió:
            On Apr 5, 11:39 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
            wrote:
            > v = os.spawnl(os.P_ NOWAIT, "mencoder", "mencoder",
            >"/Users/henriklied/test.mov", "-ofps", "25", "-o", "...")
            >>
            Thanks for your reply - but I'm afraid to tell you that spawnl didn't
            do the trick either.
            Here's the full command I used: http://dpaste.com/hold/7982/
            What means "didnt do the trick"? Do you get an exception? what's the
            returned value?
            Does the command work OK from the console?
            Try from the python interpreter, using P_WAIT, and inspect the returned
            value.
            I'd still love to get a working example of my problem using the
            Subprocess module. :-)
            The same thing:
            p = subprocess.Pope n(["mencoder", "/users/...", "-ofps", ...])

            --
            Gabriel Genellina

            Comment

            • Henrik Lied

              #7
              Re: Problems with os.spawnv

              On Apr 6, 12:09 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
              wrote:
              En Thu, 05 Apr 2007 18:53:04 -0300, Henrik Lied <henrikl...@gma il.com>
              escribió:
              >
              On Apr 5, 11:39 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
              wrote:
              v = os.spawnl(os.P_ NOWAIT, "mencoder", "mencoder",
              "/Users/henriklied/test.mov", "-ofps", "25", "-o", "...")
              >
              Thanks for your reply - but I'm afraid to tell you that spawnl didn't
              do the trick either.
              Here's the full command I used:http://dpaste.com/hold/7982/
              >
              What means "didnt do the trick"? Do you get an exception? what's the
              returned value?
              Does the command work OK from the console?
              Try from the python interpreter, using P_WAIT, and inspect the returned
              value.
              That's what I've done. P_WAIT returned a the PID 127 - but there's
              still no sign of the FLV-file, I'm afraid.

              >
              I'd still love to get a working example of my problem using the
              Subprocess module. :-)
              >
              The same thing:
              p = subprocess.Pope n(["mencoder", "/users/...", "-ofps", ...])
              >
              That example looked great at first, but on a closer look it didn't
              quite end up to be what I wanted. In a real environment the user still
              had to wait for the command to finish.
              --
              Gabriel Genellina


              I'm not sure what to do. I'm thinking of simply creating a crontab
              which checks the database every five minutes or so, and converts the
              videos that aren't converted.

              But if anyone has a good and easy solution in Python, I'd rather have
              that. I dare to think that people here will understand that.

              Comment

              • Gabriel Genellina

                #8
                Re: Problems with os.spawnv

                Henrik Lied wrote:
                Does the command work OK from the console?
                Try from the python interpreter, using P_WAIT, and inspect the returned
                value.
                That's what I've done. P_WAIT returned a the PID 127 - but there's
                still no sign of the FLV-file, I'm afraid.
                This does not look like a Python issue then; you'll have to find the
                problem elsewhere. Perhaps the spawned process is blocked waiting for
                user input, by example?

                --
                Gabriel Genellina

                Comment

                • Klaas

                  #9
                  Re: Problems with os.spawnv

                  On Apr 5, 3:25 pm, "Henrik Lied" <henrikl...@gma il.comwrote:
                  I'd still love to get a working example of my problem using the
                  Subprocess module. :-)
                  >
                  The same thing:
                  p = subprocess.Pope n(["mencoder", "/users/...", "-ofps", ...])
                  >
                  That example looked great at first, but on a closer look it didn't
                  quite end up to be what I wanted. In a real environment the user still
                  had to wait for the command to finish.
                  Then you are not using it correctly. subprocess.Pope n() returns
                  immediately. Notice the order of events here:

                  In [2]: subprocess.Pope n('sleep 2; echo foo', shell=True); print
                  'bar'
                  bar
                  foo

                  -Mike

                  Comment

                  Working...