PHP How do I implement queue processing in php

你说的曾经没有我的故事 提交于 2020-01-19 20:27:06

问题


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?


回答1:


You could use something like Zero MQ

See Example by Rasmus Lerdorf.

You could also consider using Gearman to distribute the load.




回答2:


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, install enqueue/fs library. To summarize:

composer require enqueue/simple-client enqueue/fs 

Now let's see how you can send messages from your POST script:

<?php
// producer.php

use Enqueue\SimpleClient\SimpleClient;

include __DIR__.'/vendor/autoload.php';

$client = new SimpleClient('file://'); // the queue will store messages in tmp folder

$client->sendEvent('a_topic', 'aMessageData');

The consumption script:

<?php
// consumer.php

use Enqueue\SimpleClient\SimpleClient;
use Enqueue\Psr\PsrProcessor;
use Enqueue\Psr\PsrMessage;

include __DIR__.'/vendor/autoload.php';

$client = new SimpleClient('file://');

$client->bind('a_topic', 'a_processor_name', function(PsrMessage $psrMessage) {
   // processing logic here

   return PsrProcessor::ACK;
});

// this call is optional but it worth to mention it.
// it configures a broker, for example it can create queues and excanges on RabbitMQ side. 
$client->setupBroker();

$client->consume();

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.




回答3:


Take a look at this.

It uses memcached for persistence.




回答4:


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.




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/11357187/php-how-do-i-implement-queue-processing-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!