I want the data sent by my clients (via post) to be placed in a queue and a php script on my server first checks if the queue is empty. If the queue is not empty, then the script shall process all the data in the queue one by one.How do I do this?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Problem with cronjob approach is, cronjob could at the most set to 1 minute interval, so there is a delay of 1 minute in the job execution, if that's acceptable then that's fine otherwise should use queues with polling script.
Take a look at this.
It uses memcached for persistence.
You could use something like Zero MQ
See Example by Rasmus Lerdorf.
You could also consider using Gearman to distribute the load.
Here is another great tutorial for this:
http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project/
The other solution is using Gearman which they seem to have incorporated into PHP (it wasn't the last time I played with it): http://php.net/manual/en/book.gearman.php
Since relational DB's (Ex: MySQL) are so flexible, and well understood by web developers, they are used for many type of job queues. Many PHP applications use this solution as a fallback when object caching is unconfigured. It's the method of last resort because it's a very expensive way to implement a queue.
If you must use MySQL as your queue, one of the Percona engineers wrote this blog entry on managing the potential pain points.
If you want the most scalable implementation, I would highly recommend ZeroMQ. However it's not a default, or particularly common, PHP extension. So for a project where you would not be controlling the web server stack: Use APC Objects, Memcache or Memcached, and then fallback to a MySQL cache table.
This is something you could easily do with enqueue library. First, you can choose from a variety of transports, such as AMQP, STOMP, Redis, Amazon SQS, Filesystem and so on.
Secondly, That's super easy to use. Let's start from installation:
You have to install the
enqueue/simple-client
library and one of the transports. Assuming you choose the filesystem one, installenqueue/fs
library. To summarize:Now let's see how you can send messages from your POST script:
The consumption script:
Run as many
consumer.php
processes as you by using supervisord or other process managers, on local computer you can run it without any extra libs or packages.That's a basic example and enqueue has a lot of other features that might come in handy. If you are interested, check the enqueue documentation out.