makasim/php-fpm-queue

使用php-fpm作为简单的内置异步队列

0.1.2 2018-12-19 10:52 UTC

This package is auto-updated.

Last update: 2024-08-29 04:54:29 UTC


README

使用php-fpm作为简单的内置异步队列。基于可互操作的队列接口 Queue Interop

用法

composer makasim/php-fpm-queue:0.1.x-dev queue-interop/queue-interop:0.7.x-dev enqueue/dsn:0.9.x-dev

发送脚本

<?php
# sender.php

use Makasim\PhpFpm\PhpFpmConnectionFactory;

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

$context = (new PhpFpmConnectionFactory('tcp://localhost:9000'))->createContext();

$queue = $context->createQueue('/app/worker.php');
$message = $context->createMessage('aBody');

$context->createProducer()->send($queue, $message);

工作脚本

<?php
# worker.php

use Makasim\PhpFpm\PhpFpmConnectionFactory;

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

$context = (new PhpFpmConnectionFactory('tcp://localhost:9000'))->createContext();
// or
//$context = (new PhpFpmConnectionFactory('unix:///var/run/php/php7.1-fpm.sock'))->createContext(); 

$queue = $context->createQueue(__FILE__);

$consumer = $context->createConsumer($queue);

if ($message = $consumer->receiveNoWait()) {
    // process message

    $consumer->acknowledge($message);
}

启动PHP-FPM

docker run -v `pwd`:/app -p 9000:9000 php:7.2-fpm

发送消息

php sender.php

致谢

受Benjamin的帖子 使用php-fpm作为简单的内置异步队列 启发

许可

MIT许可