PHP 如何在 php 中实现队列处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11357187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:20:14  来源:igfitidea点击:

PHP How do I implement queue processing in php

phpqueue

提问by ASHUTOSH

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?

我希望将我的客户端发送的数据(通过 post)放在一个队列中,我服务器上的 php 脚本首先检查队列是否为空。如果队列不为空,那么脚本会一一处理队列中的所有数据。我该怎么做?

回答by Bernhard Kircher

You could use something like Zero MQ

你可以使用像Zero MQ这样的东西

See Example by Rasmus Lerdorf.

参见Rasmus Lerdorf 的例子

You could also consider using Gearmanto distribute the load.

您也可以考虑使用Gearman来分配负载。

回答by Maksim Kotlyar

This is something you could easily do with enqueuelibrary. First, you can choose from a variety of transports, such as AMQP, STOMP, Redis, Amazon SQS, Filesystem and so on.

这是您可以使用enqueue库轻松完成的事情。首先,您可以选择多种传输方式,例如 AMQP、STOMP、Redis、Amazon SQS、Filesystem 等。

Secondly, That's super easy to use. Let's start from installation:

其次,这非常易于使用。让我们从安装开始:

You have to install the enqueue/simple-clientlibrary and one of the transports. Assuming you choose the filesystem one, install enqueue/fslibrary. To summarize:

您必须安装enqueue/simple-client库和传输之一。假设您选择文件系统之一,安装enqueue/fs库。总结一下:

composer require enqueue/simple-client enqueue/fs 

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

现在让我们看看如何从 POST 脚本发送消息:

<?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.phpprocesses as you by using supervisordor other process managers, on local computer you can run it without any extra libs or packages.

consumer.php使用supervisord或其他进程管理器运行尽可能多的进程,在本地计算机上运行它,无需任何额外的库或包。

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 documentationout.

这是一个基本示例,enqueue 有许多其他可能会派上用场的功能。如果您有兴趣,请查看enqueue 文档

回答by nullpotent

Take a look at this.

看看这个

It uses memcached for persistence.

它使用 memcached 进行持久化。

回答by Faisal Ameer

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.

cronjob 方法的问题是,cronjob 最多可以设置为 1 分钟的间隔,因此作业执行会有 1 分钟的延迟,如果可以接受,那就没问题,否则应该使用带有轮询脚本的队列。

回答by J. M. Becker

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.

由于关系数据库(例如:MySQL)非常灵活,并且被 Web 开发人员很好地理解,因此它们被用于许多类型的作业队列。当未配置对象缓存时,许多 PHP 应用程序使用此解决方案作为后备。这是最后的方法,因为它是实现队列的一种非常昂贵的方法。

If you must use MySQL as your queue, one of the Percona engineers wrote this blog entryon managing the potential pain points.

如果你必须使用 MySQL 作为你的队列,Percona 的一位工程师写了这篇关于管理潜在痛点的博客文章

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.

如果你想要最可扩展的实现,我强烈推荐 ZeroMQ。然而,它不是默认的或特别常见的 PHP 扩展。因此,对于您不会控制 Web 服务器堆栈的项目:使用 APC 对象、Memcache 或 Memcached,然后回退到 MySQL 缓存表。

回答by Brian

Here is another great tutorial for this:

这是另一个很棒的教程:

http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project/

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

另一个解决方案是使用 Gearman,他们似乎已将其合并到 PHP 中(这不是我最后一次使用它):http: //php.net/manual/en/book.gearman.php