Problems with os.spawnv

H

Henrik Lied

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.
 
K

kyosohma

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.Popen 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
 
H

Henrik Lied

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.Popen 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!
 
G

Gabriel Genellina

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", "...")
 
H

Henrik Lied

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", "...")

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. :)
 
G

Gabriel Genellina

On Apr 5, 11:39 pm, "Gabriel Genellina" <[email protected]>
wrote:
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.Popen(["mencoder", "/users/...", "-ofps", ...])
 
H

Henrik Lied

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.Popen(["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.



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.
 
G

Gabriel Genellina

Henrik said:
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?
 
K

Klaas

The same thing:
p = subprocess.Popen(["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.Popen() returns
immediately. Notice the order of events here:

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

-Mike
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top