Table of Contents
Beanstalkd introduction
What is Beanstalkd
Beanstalkd running process
Beanstalkd installation
Pheanstalk installation
Pheanstalk operation
Connect Beanstalkd
Beanstalkd status method
Beanstalkd producer method
Beanstalkd consumer method
Home Backend Development PHP Tutorial Methods and parameter comments for operating Beanstalkd in PHP

Methods and parameter comments for operating Beanstalkd in PHP

Jul 06, 2018 pm 04:52 PM
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*
Copy after login

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*
Copy after login

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);
?>
Copy after login

Beanstalkd status method

Beanstalkd status

$stats = $pheanstalk->stats();
Copy after login

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
        )
)
Copy after login

Current pipeline list

$listTubes = $pheanstalk->listTubes();
Copy after login

View pipeline details

$stats = $pheanstalk->statsTube();
Copy after login

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
        )
)
Copy after login

View task details

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

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 //释放预留任务次数
        )
)
Copy after login

View task details (by ID)

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

Beanstalkd producer method

Specify the pipeline to be used

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

Insert data into the pipe

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

Or:

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

Beanstalkd consumer method

Listening pipe

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

Remove pipes that do not need to be monitored

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

Listen to the pipe in a blocked manner

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

List all monitored pipes

$pheanstalk->listTubesWatched();
Copy after login

watch reserve method

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

Delete the current task

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

Reset the current task Put it into the pipe

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

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
Copy after login

Reserve the task

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

Release the reserved task (change to reday status)

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

Release reserved tasks in batches

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

Read the currently ready tasks (ready)

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

Read the task currently in delayed state (delayed)

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

Set delay for pipeline

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

Cancel the delay on the pipeline

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

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles