#
知识 2024-03-19

在php中使用celery

By alex 471 Views 12 MIN READ 26 Comments

包地址:smuuf/celery-for-php: A modern PHP client library for Celery. (github.com)

Requirements

  • PHP 8.0+

Installation

Install celery-for-php via Composer.

composer require smuuf/celery-for-php

Redis (Predis)

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.

Example usage

<?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

AMQP/RabbitMQ (PhpAmqpLib)

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.

Example usage

<?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 原创

采用 CC BY-NC-SA 4.0 协议进行许可

转载请注明出处:https://blog.laialex.com/archives/27/

TAGS: 无标签

YOU MAY ALSO LIKE

  • 暂无相关推荐,看看别的吧。

26 COMMENTS

LEAVE A REPLY

  1. antibiotics augmentin ca
    2025-03-07 05:46

    The subordinates who fought with Zhao Ling were more excited than each other buy augmentin 500 mg onlinbe

  2. uk topical finasteride
    2025-03-13 05:27

    Do curcumin and turmeric supplements have possible side effects propecia online australia Long term adjuvant therapy with tamoxifen

  3. THC gummies
    2025-07-09 02:08

    Good shout.

  4. ace ultra premium mini
    2025-07-11 02:25

    Nice

  5. thc gummies
    2025-07-12 00:14

    thc gummies

  6. where to buy thca
    2025-07-12 00:15

    where to buy thca area 52

  7. thcv gummies
    2025-07-12 00:36

    thcv gummies area 52

  8. best cbd sleep edibles
    2025-07-12 00:37

    best cbd sleep edibles area 52

  9. best disposable vaporizers
    2025-07-12 01:15

    best disposable vaporizers area 52

  10. full spectrum cbd gummies
    2025-07-12 01:24

    full spectrum cbd gummies area 52

  11. disposable weed pen
    2025-07-12 01:35

    disposable weed pen area 52

  12. live resin carts
    2025-07-12 02:15

    live resin carts area 52

  13. liquid thc
    2025-07-12 02:17

    liquid thc area 52

  14. live resin gummies
    2025-07-12 02:27

    live resin gummies area 52

  15. hybrid gummies
    2025-07-12 02:40

    hybrid gummies area 52

  16. magic mushrooms
    2025-07-12 02:44

    magic mushrooms area 52

  17. buy pre rolls online
    2025-07-12 02:55

    buy pre rolls online area 52

  18. live resin
    2025-07-12 03:11

    live resin area 52

  19. liquid diamonds
    2025-07-12 03:15

    liquid diamonds area 52

  20. mood thc gummies
    2025-07-12 03:25

    mood thc gummies area 52

  21. microdose thc
    2025-07-12 03:39

    microdose thc area 52

  22. thc oil
    2025-07-12 03:41

    thc oil area 52

  23. best indica thc weed pens
    2025-07-12 03:41

    best indica thc weed pens area 52

  24. snow caps weed
    2025-07-12 03:42

    snow caps weed area 52

  25. best sativa thc carts
    2025-07-12 04:00

    best sativa thc carts area 52

  26. 2 gram carts
    2025-07-12 04:04

    2 gram carts area 52