Monday, February 28, 2011

Re: how to redirect and let the server do some lengthy job in the background?

On Feb 28, 2011, at 04:14, AD7six wrote:
> On Feb 28, 8:14 am, Zaky Katalan-Ezra wrote:
>> On a Linux machine create a bash file like this
>> -----
>> #!/bin/sh
>>
>> ffmpeg -i $1 -r $2 $3 &
>>
>> -----
>> The ampersand at the end tell the bash to run in the background.
>> the $n is parameters.
>>
>> From php call it like this:
>> $command = mybashpath.sh inputfile.mpg 24 outputfile.flv;
>> *exec* ( $command );
>
> That's not going to work.
>
> If the calling process (php http request) finishes before the child
> ("ffmpeg ... &") then your child process will die immediately also.
> For your example you'd want
>
> exec('nohup ffmpeg ... &');

This might depend on which web server you're using, and its settings. Some web servers keep long-running processes that handle many requests in a row, others might spawn a new process for each request.


> however, IMO you're better off using a real queue system, if for no
> other reason that it makes pushing logic into background processes so
> much easier in general (sending emails, processing files, reindexing
> data etc. etc.)

Right. If you have 100 users upload their videos simultaneously, that's great, but you probably don't want to then start 100 simultaneous video encode jobs; your CPU will be overloaded, probably you'll run out of memory too and slow things down further as you start thrashing virtual memory. Better to enqueue each video encode request (e.g. by creating a record in the videos table and setting the column encoded=0), and then have a separate daemon that dequeues videos and encodes them (and marks them as encoded=1) one at a time (or however many your system can handle at once, given how many CPU cores and how much memory it has).


--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: