在php中使用celery
包地址:smuuf/celery-for-php: A modern PHP client library for Celery. (github.com)
- PHP 8.0+
Install celery-for-php via Composer.
composer require smuuf/celery-for-php
If you want to use Redis as a broker and/or result backend, celery-for-php contains a Redis driver backed by Predis
.
The Predis Client
object then needs to be wrapped in our Smuuf\CeleryForPhp\Drivers\PredisRedisDriver
driver object, which provides the necessary interface for celery-for-php's actual communication with Redis.
<?php
use Predis\Client as PredisClient;
use Smuuf\CeleryForPhp\Celery;
use Smuuf\CeleryForPhp\TaskSignature;
use Smuuf\CeleryForPhp\Brokers\RedisBroker;
use Smuuf\CeleryForPhp\Drivers\PredisRedisDriver;
use Smuuf\CeleryForPhp\Backends\RedisBackend;
$predis = new PredisClient(['host' => '127.0.0.1']);
$redisDriver = new PredisRedisDriver($predis);
$celery = new Celery(
new RedisBroker($redisDriver),
new RedisBackend($redisDriver),
// Optionally explicit config object.
// config: new \Smuuf\CeleryForPhp\Config(...)
);
$task = new TaskSignature(
taskName: 'my_celery_app.add_numbers',
queue: 'my_queue', // Optional, 'celery' by default.
args: [1, 3, 5],
// kwargs: ['arg_a' => 123, 'arg_b' => 'something'],
// eta: 'now +10 minutes',
// ... or more optional arguments.
);
// Send the task into Celery.
$asyncResult = $celery->sendTask($task);
// Wait for the result (up to 10 seconds by default) and return it.
// Alternatively a \Smuuf\CeleryForPhp\Exc\CeleryTimeoutException exception will
// be thrown if the task won't finish in time.
$result = $asyncResult->get();
// $result === 9
You can use AMQP/RabbitMQ as the broker instead, with Redis as the backend. celery-for-php contains a AMQP driver backed by PhpAmqpLib
.
The PhpAmqpLib AMQPConnection
or AMQPSSLConnection
object needs to be wrapped in our Smuuf\CeleryForPhp\Drivers\PhpAmqpLibAmqpDriver
driver object, which provides the necessary interface for celery-for-php's actual communication via AMQP.
<?php
use Predis\Client as PredisClient;
use Smuuf\CeleryForPhp\Celery;
use Smuuf\CeleryForPhp\TaskSignature;
use Smuuf\CeleryForPhp\Brokers\AmqpBroker;
use Smuuf\CeleryForPhp\Drivers\PredisRedisDriver;
use Smuuf\CeleryForPhp\Drivers\PhpAmqpLibAmqpDriver;
use PhpAmqpLib\Connection\AMQPSSLConnection;
use Smuuf\CeleryForPhp\Backends\RedisBackend;
//$amqpConn = new AMQPConnection(['127.0.0.1', '5672', '', '', '/']);
$amqpConn = new AMQPSSLConnection(['127.0.0.1', '5672', '', '', '/', ['verify_peer'=>false]]);
$amqpDriver = new PhpAmqpLibAmqpDriver($amqpConn);
$predis = new PredisClient(['host' => '127.0.0.1']);
$redisDriver = new PredisRedisDriver($predis);
$celery = new Celery(
new AmqpBroker($amqpDriver),
new RedisBackend($redisDriver),
// Optionally explicit config object.
// config: new \Smuuf\CeleryForPhp\Config(...)
);
$task = new TaskSignature(
taskName: 'my_celery_app.add_numbers',
queue: 'my_queue', // Optional, 'celery' by default.
args: [1, 3, 5],
// kwargs: ['arg_a' => 123, 'arg_b' => 'something'],
// eta: 'now +10 minutes',
// ... or more optional arguments.
);
// Send the task into Celery.
$asyncResult = $celery->sendTask($task);
// Wait for the result (up to 10 seconds by default) and return it.
// Alternatively a \Smuuf\CeleryForPhp\Exc\CeleryTimeoutException exception will
// be thrown if the task won't finish in time.
$result = $asyncResult->get();
// $result === 9
本文系作者 @alex 原创发布在Alex站点。未经许可,禁止转载。
全部评论 26
antibiotics augmentin ca
Google Chrome Windows 10uk topical finasteride
Google Chrome Windows 10THC gummies
FireFox Ubuntuace ultra premium mini
Google Chrome MacOSthc gummies
Google Chrome Windows 10where to buy thca
Google Chrome Windows 10thcv gummies
Google Chrome MacOSbest cbd sleep edibles
FireFox MacOSbest disposable vaporizers
Google Chrome MacOSfull spectrum cbd gummies
Google Chrome Windows 10disposable weed pen
Google Chrome Windows 10live resin carts
Google Chrome MacOSliquid thc
Google Chrome MacOSlive resin gummies
FireFox MacOShybrid gummies
Google Chrome MacOSmagic mushrooms
Google Chrome MacOSbuy pre rolls online
Google Chrome Windows 10live resin
Safari MacOSliquid diamonds
Safari MacOSmood thc gummies
Google Chrome Windows 10microdose thc
Google Chrome Windows 10thc oil
Google Chrome Windows 10best indica thc weed pens
FireFox Windows 10snow caps weed
Google Chrome Linuxbest sativa thc carts
Google Chrome Windows 102 gram carts
Google Chrome Windows 10