views:

552

answers:

4

I'm working on a PHP web interface that will receive huge traffic. Some insert/update requests will contain images that will have to be resized to some common sizes to speed up their further retrieval.

One way to do it is probably to set up some asynchronous queue on the server. Eg. set up a table in a db with a tasks queue that would be populated by PHP requests and let some other process on the server watch the table and process any waiting tasks. How would you do that? What would be the proper environment for that long running process? Java, or maybe something lighter would do?

+10  A: 

If what you're doing is really high volume then what you're looking for is something like beanstalkd. It is a distributed work queue processor. You just put a job on the queue and then forget about it.

Of course then you need something at the other end reading the queue and processing the work. There are multiple ways of doing this.

The easiest is probably to have a cron job that runs sufficiently often to read the work queue and process the requests. Alternatively you can use some kind of persistent daemon process that is woken up by work becoming available.

The advantage of this kind of approach is you can tailor the number of workers to how much work needs to get done and beanstalkd handles distributed prorcessing (in the sense that the listners can be on different machines).

cletus
I recently implemented a Beanstalkd/PHP queue processor, and as one part of that, an image resizer. 2 weeks and 950K jobs later (with 3600+ resizes), it's usually completing the resizing in less time than it takes to return the page.
Alister Bulman
+2  A: 

You may set a cron task that would check the queue table. The script that handles actions waiting in the queue can be written e.g. in php so you don't have to change implementation language.

empi
+1  A: 

You would want to create a daemon which would "sleep" for a period of time and then check the database for items to process. Once it found items to process, it would process them and then check again as soon as it was done, if no more, then sleep. You can create daemon's in any language, including PHP.

Alternatively, you can just have PHP execute a script and continue on. So that PHP won't wait for the script to finish before continuing, execute it in the background.

exec("nohup /usr/bin/php -f /path/to/script/script.php > /dev/null 2>&1 &");

Although you have to be careful with that since you could end up having too many processes running in the background since there is no queueing.

Brent Baisley
+1  A: 

I use Perl for long running process in combination with beanstalkd. The nice thing is that the Beanstalkd client for Perl has a blocking reserve method. This way it uses almost no CPU time when there is nothing to do. But when it has to do its job, it will automatically start processing. Very efficient.

Peter Stuifzand
Alister Bulman