在php中使用celery
评论 26 热度 423
包地址: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
antibiotics augmentin ca Windows 10 · Google Chrome
The subordinates who fought with Zhao Ling were more excited than each other buy augmentin 500 mg onlinbe
#72433 2025-03-07 05:46 回复
uk topical finasteride Windows 10 · Google Chrome
Do curcumin and turmeric supplements have possible side effects propecia online australia Long term adjuvant therapy with tamoxifen
#77872 2025-03-13 05:27 回复
THC gummies Ubuntu · FireFox
Good shout.
#174466 2025-07-09 02:08 回复
ace ultra premium mini MacOS · Google Chrome
Nice
#180469 2025-07-11 02:25 回复
thc gummies Windows 10 · Google Chrome
thc gummies
#183502 2025-07-12 00:14 回复
where to buy thca Windows 10 · Google Chrome
where to buy thca area 52
#183503 2025-07-12 00:15 回复
thcv gummies MacOS · Google Chrome
thcv gummies area 52
#183506 2025-07-12 00:36 回复
best cbd sleep edibles MacOS · FireFox
best cbd sleep edibles area 52
#183507 2025-07-12 00:37 回复
best disposable vaporizers MacOS · Google Chrome
best disposable vaporizers area 52
#183517 2025-07-12 01:15 回复
full spectrum cbd gummies Windows 10 · Google Chrome
full spectrum cbd gummies area 52
#183518 2025-07-12 01:24 回复
disposable weed pen Windows 10 · Google Chrome
disposable weed pen area 52
#183520 2025-07-12 01:35 回复
live resin carts MacOS · Google Chrome
live resin carts area 52
#183529 2025-07-12 02:15 回复
liquid thc MacOS · Google Chrome
liquid thc area 52
#183530 2025-07-12 02:17 回复
live resin gummies MacOS · FireFox
live resin gummies area 52
#183531 2025-07-12 02:27 回复
hybrid gummies MacOS · Google Chrome
hybrid gummies area 52
#183535 2025-07-12 02:40 回复
magic mushrooms MacOS · Google Chrome
magic mushrooms area 52
#183536 2025-07-12 02:44 回复
buy pre rolls online Windows 10 · Google Chrome
buy pre rolls online area 52
#183539 2025-07-12 02:55 回复
live resin MacOS · Safari
live resin area 52
#183543 2025-07-12 03:11 回复
liquid diamonds MacOS · Safari
liquid diamonds area 52
#183544 2025-07-12 03:15 回复
mood thc gummies Windows 10 · Google Chrome
mood thc gummies area 52
#183546 2025-07-12 03:25 回复
microdose thc Windows 10 · Google Chrome
microdose thc area 52
#183550 2025-07-12 03:39 回复
thc oil Windows 10 · Google Chrome
thc oil area 52
#183551 2025-07-12 03:41 回复
best indica thc weed pens Windows 10 · FireFox
best indica thc weed pens area 52
#183552 2025-07-12 03:41 回复
snow caps weed Linux · Google Chrome
snow caps weed area 52
#183553 2025-07-12 03:42 回复
best sativa thc carts Windows 10 · Google Chrome
best sativa thc carts area 52
#183556 2025-07-12 04:00 回复
2 gram carts Windows 10 · Google Chrome
2 gram carts area 52
#183557 2025-07-12 04:04 回复