467,081 Members | 1,251 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

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.

Apr 5 '07 #1
  • viewed: 1792
Share:
8 Replies
On Apr 5, 4:19 pm, "Henrik Lied" <henrikl...@gmail.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.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

Apr 5 '07 #2
On Apr 5, 11:33 pm, kyoso...@gmail.com wrote:
On Apr 5, 4:19 pm, "Henrik Lied" <henrikl...@gmail.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.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!

Apr 5 '07 #3
En Thu, 05 Apr 2007 18:19:56 -0300, Henrik Lied <he********@gmail.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

Apr 5 '07 #4
On Apr 5, 11:39 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 05 Apr 2007 18:19:56 -0300, Henrik Lied <henrikl...@gmail.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. :-)

Apr 5 '07 #5
En Thu, 05 Apr 2007 18:53:04 -0300, Henrik Lied <he********@gmail.com>
escribió:
On Apr 5, 11:39 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
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.Popen(["mencoder", "/users/...", "-ofps", ...])

--
Gabriel Genellina

Apr 5 '07 #6
On Apr 6, 12:09 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 05 Apr 2007 18:53:04 -0300, Henrik Lied <henrikl...@gmail.com>
escribió:
On Apr 5, 11:39 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
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.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.
--
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.

Apr 5 '07 #7
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

Apr 6 '07 #8
On Apr 5, 3:25 pm, "Henrik Lied" <henrikl...@gmail.comwrote:
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.
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

Apr 7 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by nushin | last post: by
3 posts views Thread by Florian Lindner | last post: by
5 posts views Thread by alexLIGO | last post: by
reply views Thread by IamIan | last post: by
3 posts views Thread by IamIan | last post: by
reply views Thread by Chris Reuter | last post: by
2 posts views Thread by Fabio Chelly | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.