search
HomeBackend DevelopmentPHP TutorialMethods and parameter comments for operating Beanstalkd in PHP

This article mainly introduces the methods and parameter comments about operating Beanstalkd in PHP. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

Beanstalkd introduction

What is Beanstalkd

Beanstalk is a high-performance, lightweight distributed memory queue system. It was originally designed to reduce the page access of high-volume web application systems by asynchronously executing time-consuming tasks in the background. Delay, supported the Facebook Causes application with 9.5 million users.
Later open sourced, PostRank is now deployed and used on a large scale, processing millions of tasks every day. Beanstalkd is a typical Memcached-like design. The protocol and usage are the same, so users who have used memcached will feel that Beanstalkd looks familiar.

Beanstalkd running process

job typical life cycle

   put            reserve               delete
  -----> [READY] ---------> [RESERVED] --------> *poof*

job possible state migration

   put with delay               release with delay
  ----------------> [DELAYED] <------------.
                        |                   |
                 kick   | (time passes)     |
                        |                   |
   put                  v     reserve       |       delete
  -----------------> [READY] ---------> [RESERVED] --------> *poof*
                       ^  ^                |  |
                       |   \  release      |  |
                       |    `-------------&#39;   |
                       |                      |
                       | kick                 |
                       |                      |
                       |       bury           |
                    [BURIED] <---------------&#39;
                       |
                       |  delete
                        `--------> *poof*

Beanstalkd installation

Skip the installation, if necessary please refer to click reference

Note: Composer needs to be installed, tutorial link Composer installation method

Pheanstalk installation

The PHP third-party library used to operate Beanstalkd, the installation is skipped, if necessary, please refer to the reference

Pheanstalk operation

Connect Beanstalkd

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;

use Pheanstalk\Pheanstalk;

/**
 * 实例化beanstalk
 * 参数依次为:ip地址 端口号默认11300 连接超时时间 是否长连接
 */
$pheanstalk = new Pheanstalk(&#39;127.0.0.1&#39;, 11300, 3, false);
?>

Beanstalkd status method

Beanstalkd status

$stats = $pheanstalk->stats();

Return:

Pheanstalk\Response\ArrayResponse Object
(
    [_name:Pheanstalk\Response\ArrayResponse:private] => OK
    [storage:ArrayObject:private] => Array
        (
            [current-jobs-urgent] => 0 //当前存在优先级的任务数
            [current-jobs-ready] => 0 //当前准备就绪的任务数
            [current-jobs-reserved] => 0 //当前处于阻塞的任务数
            [current-jobs-delayed] => 0 //当前处于延迟状态的任务数
            [current-jobs-buried] => 0 //当前预留的任务数
            [cmd-put] => 0 //cmd命令为累计运行次数
            [cmd-peek] => 0
            [cmd-peek-ready] => 0
            [cmd-peek-delayed] => 0
            [cmd-peek-buried] => 0
            [cmd-reserve] => 0
            [cmd-reserve-with-timeout] => 0
            [cmd-delete] => 0
            [cmd-release] => 0
            [cmd-use] => 0
            [cmd-watch] => 0
            [cmd-ignore] => 0
            [cmd-bury] => 0
            [cmd-kick] => 0
            [cmd-touch] => 0
            [cmd-stats] => 1
            [cmd-stats-job] => 0
            [cmd-stats-tube] => 0
            [cmd-list-tubes] => 3
            [cmd-list-tube-used] => 0
            [cmd-list-tubes-watched] => 0
            [cmd-pause-tube] => 0
            [job-timeouts] => 0 //超时的任务
            [total-jobs] => 0 //任务总数
            [max-job-size] => 65535 //任务字符串大小
            [current-tubes] => 1 //当前的管道数
            [current-connections] => 1 //当前打开的连接数
            [current-producers] => 0 //当前生产者数量
            [current-workers] => 0 //当前消费者数量
            [current-waiting] => 0 //发出reserved指令,但没有响应的数量
            [total-connections] => 4 //累计链接数量
            [pid] => 530 //Beanstalkd进程id
            [version] => 1.10
            [rusage-utime] => 0.003545
            [rusage-stime] => 0.007473
            [uptime] => 105855 //运行时间(秒)
            [binlog-oldest-index] => 0
            [binlog-current-index] => 0
            [binlog-records-migrated] => 0
            [binlog-records-written] => 0
            [binlog-max-size] => 10485760
            [id] => 4199e3eca8bfdea8
            [hostname] => lmmlwendeMacBook-Air.local
        )
)

Current pipeline list

$listTubes = $pheanstalk->listTubes();

View pipeline details

$stats = $pheanstalk->statsTube();

Return:

Pheanstalk\Response\ArrayResponse Object
(
    [_name:Pheanstalk\Response\ArrayResponse:private] => OK
    [storage:ArrayObject:private] => Array
        (
            [name] => default //当前管道名
            [current-jobs-urgent] => 0 //当前管道存在优先级的任务数
            [current-jobs-ready] => 0 //当前管道准备就绪的任务书
            [current-jobs-reserved] => 0 //当前管道处于阻塞的任务数
            [current-jobs-delayed] => 0 //当前管道处于延迟状态的任务数
            [current-jobs-buried] => 0 //当前管道预留的任务数
            [total-jobs] => 0 //当前管道总任务数
            [current-using] => 1 //当前管道生产者数量
            [current-watching] => 1 //当前管道消费者数量
            [current-waiting] => 0 //发出reserved指令,但没有响应的数量
            [cmd-delete] => 0
            [cmd-pause-tube] => 0
            [pause] => 0
            [pause-time-left] => 0
        )
)

View task details

$job =  $pheanstalk->watch(&#39;default&#39;)->reserve();
$job_stats = $pheanstalk->statsJob($job);

Return:

Pheanstalk\Response\ArrayResponse Object
(
    [_name:Pheanstalk\Response\ArrayResponse:private] => OK
    [storage:ArrayObject:private] => Array
        (
            [id] => 1 //任务job ID
            [tube] => test //所处管道
            [state] => reserved //当前状态
            [pri] => 1024 //任务优先级(默认1024)
            [age] => 469 //任务存活时间(秒)
            [delay] => 0 //任务延迟时间(秒)
            [ttr] => 60 //任务执行时间
            [time-left] => 59 //任务在reserve状态维持的秒数
            [file] => 0 // binlog-4 默认-0
            [reserves] => 2 //总共reserve次数
            [timeouts] => 0 //任务超时次数
            [releases] => 0 //重设任务次数
            [buries] => 0 //预留次数
            [kicks] => 0 //释放预留任务次数
        )
)

View task details (by ID)

$job =  $pheanstalk->peek(1);
$job_stats = $pheanstalk->statsJob($job);

Beanstalkd producer method

Specify the pipeline to be used

$tube = $pheanstalk->useTube(&#39;default&#39;);

Insert data into the pipe

$tube = $pheanstalk->useTube(&#39;default&#39;);
$put = $tube->put(
    'hello, beanstalk', // 任务内容
    1024, // 任务的优先级
    10,  // 不等待直接放到ready队列中
    60 // 处理任务的时间
);

Or:

$pheanstalk->putInTube(&#39;default&#39;,  &#39;test1&#39;, 1024, 10, 60);

Beanstalkd consumer method

Listening pipe

$tube =  $pheanstalk->watch(&#39;user&#39;);

Remove pipes that do not need to be monitored

$tube =  $pheanstalk->watch(&#39;user&#39;)->ignore(&#39;default&#39;);

Listen to the pipe in a blocked manner

$job =  $pheanstalk->watch(&#39;user&#39;)->reserve(4); //堵塞时间为4秒

List all monitored pipes

$pheanstalk->listTubesWatched();

watch reserve method

$pheanstalk->reserveFromTube(&#39;default&#39;)

Delete the current task

$job =  $pheanstalk->watch(&#39;default&#39;)->reserve();
$pheanstalk->delete($job);

Reset the current task Put it into the pipe

$job =  $pheanstalk->watch(&#39;default&#39;)->reserve();
$pheanstalk->release($job);

to extend the life of the task (when the time to process the task is less than the current task execution time)

$job =  $pheanstalk->watch(&#39;default&#39;)->reserve();
$pheanstalk->touch($job);
//TODO

Reserve the task

$job =  $pheanstalk->watch(&#39;default&#39;)->reserve();
$pheanstalk->bury($job);

Release the reserved task (change to reday status)

$job = $pheanstalk->peekBuried(&#39;default&#39;);
$pheanstalk->kickJob($job);

Release reserved tasks in batches

$pheanstalk->userTube(&#39;default&#39;)->kick(999); //将id小于999的预留任务全部释放

Read the currently ready tasks (ready)

$job = $pheanstalk->peekReady(&#39;default&#39;);

Read the task currently in delayed state (delayed)

$job = $pheanstalk->peekDelayed(&#39;default&#39;);

Set delay for pipeline

$pheanstalk->pauseTube(&#39;default&#39;, 100); //设置100秒延迟

Cancel the delay on the pipeline

$pheanstalk->resumeTube(&#39;default&#39;);

The above is the entire content of this article, I hope it will be useful for everyone’s learning Help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

The intranet penetration application Spike implemented by PHP has been reconstructed

php calls phantomjs to WeChat Mini program sharing

The above is the detailed content of Methods and parameter comments for operating Beanstalkd in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools