CodeIgniter Forums

Full Version: Run external program in background
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]eroy4u[/eluser]
In my web site, it allow people to upload an video and then call an external program to encode the video to flash video format.
I use exec('...') to open the external program for encoding.
However, encoding takes much time so my php script runs maybe half an hour.

Is there a way I can have my php script end quickly and run this external program in background?

Thanks if anyone can help.

El Forum

[eluser]TheFuzzy0ne[/eluser]
If your server is on a Linux/Unix system, running your command like this should place it in the background.

exec("yourProgram &");

The only problem is that depending on how you handle it, there is a chance that you could have multiple instances running in the background, so it's up to you to design a method of allowing some kind of queueing system for files awaiting encoding.

EDIT: You may also wish to look into popen() and proc_open(). This will allow you to process any output returned by the shell.

El Forum

[eluser]eroy4u[/eluser]
thanks, is there no way i run external program in background in Windows?

Furthermore, how can i open a new thread in php? If yes, how can different thread communicate with each other?

El Forum

[eluser]TheFuzzy0ne[/eluser]
For Winblows-based systems, I believe appending /B to the end of the command should do it. Please bear in mind that in both cases (using & or /B), you're code won't be portable.

What do you mean by "new thread"? PHP works on a single thread. Are you looking for a way to keep the PHP thread open, but disconnect the script from the browser, so the PHP script keeps running and can do stuff when the process finishes, but the user doesn't end up having to stay on the same constantly loading page for 30 minutes?

El Forum

[eluser]eroy4u[/eluser]
[quote author="TheFuzzy0ne" date="1209594896"]For Winblows-based systems, I believe appending /B to the end of the command should do it. Please bear in mind that in both cases (using & or /B), you're code won't be portable.

What do you mean by "new thread"? PHP works on a single thread. Are you looking for a way to keep the PHP thread open, but disconnect the script from the browser, so the PHP script keeps running and can do stuff when the process finishes, but the user doesn't end up having to stay on the same constantly loading page for 30 minutes?[/quote]
Yes it is what i mean. Is it possible?

El Forum

[eluser]TheFuzzy0ne[/eluser]
To be honest, I'm not entirely sure. I seem to recall that it is, but I can't remember how.

May I suggest that you instead design some kind of queueing system? If 1000 users upload a file each, you will have 1000 instances of your compression app running in the background of your Web server, which is bound to be bad news and will almost certainly affect your Web server's performance. I have seen projects like this in the past, and generally they have their own standalone platform which does the encoding. The files are sent to the platform and queued up, and the platform works through them one at a time, or several at a time. This means that the Web server shouldn't slow down too much. Ideally, you could do with 3 servers. One as a Web server, one as the encoding platform, and one for uploading files to. I accept that this isn't always possible, but hopefully it gives you something to think about and may save you running into unforeseen problems.

I am intrigued by your question however, so I will search for an answer, and post back if I find one.

Sorry, I couldn't be of any more help.

El Forum

[eluser]TheFuzzy0ne[/eluser]
One idea that popped into my head (but may not work), is to use a head redirect once the file is received by the server. I'm unsure whether the server will carry on processing the script after that, so someone else who knows may need to comment.

EDIT: the server will continue to process the script after a redirect header has been sent if it's not exited from using something like "exit;", so this should work like you want, but I still suggest using some kind of queueing system to prevent a server overload.

El Forum

[eluser]TheFuzzy0ne[/eluser]
Another possible solution may be to run a PHP script using exec(). For example php -f myfile.php, which can run pretty much in an infinite loop (as long as there are files to encode). You can use a file, or a database to store the number of processes going on, and a file queue so your calling script will execute the encoding script, and it will either start encoding if there aren't many others being encoded, or it can wait in a queue until you have the resources to do it.

It's a very simplistic idea, but unfortunately it's the best a mind as inferior as mine can come up with (for now, anyway).

I've searched online for a solution, and I've found others emulating multi-thread applications, but they all seem to have serious flaws or limits.

El Forum

[eluser]Tom Glover[/eluser]
If you need media encoding and storage try something like, http://www.nirvanix.com/. this allows you to run and store your encoded files on there servers for minimal costs, reducing the load on your server(s). Your php scripts will run for a second, because as soon as you have sent the api call to their servers you dont have to worry.

EDIT: Me 300th Post. Woop Woop!

El Forum

[eluser]eroy4u[/eluser]
[quote author="TheFuzzy0ne" date="1209599854"]Another possible solution may be to run a PHP script using exec(). For example php -f myfile.php, which can run pretty much in an infinite loop (as long as there are files to encode). You can use a file, or a database to store the number of processes going on, and a file queue so your calling script will execute the encoding script, and it will either start encoding if there aren't many others being encoded, or it can wait in a queue until you have the resources to do it.

It's a very simplistic idea, but unfortunately it's the best a mind as inferior as mine can come up with (for now, anyway).

I've searched online for a solution, and I've found others emulating multi-thread applications, but they all seem to have serious flaws or limits.[/quote]

thanks a lot. i also think this simple solution is a good one.
in fact i also want to separate my file uploading server from my web server,
however, how can i do this? as the file uploading script seems just like a normal web script
Pages: 1 2