# 隊列(Queueing)
Activities like processing videos, resizing images or sending emails aren’t suitable to be executed online or in real time because it may slow the loading time of pages and severely impact the user experience.
The best solution here is to implement background jobs. The web application puts jobs into a queue and which will be processed separately.
While you can find more sophisticated PHP extensions to address queueing in your applications like[RabbitMQ](http://pecl.php.net/package/amqp); Phalcon provides a client for[Beanstalk](http://www.igvita.com/2010/05/20/scalable-work-queues-with-beanstalk/), a job queueing backend inspired by[Memcache](http://memcached.org/). It’s simple, lightweight, and completely specialized for job queueing.
注意
Some of the returns from queue methods require that the module Yaml be installed. Please refer to[http://php.net/manual/book.yaml.php](http://php.net/manual/book.yaml.php)for more information. For PHP = 7, you will need to use Yaml >= 2.0.0.
## 將任務加入隊列(Putting Jobs into the Queue)
After connecting to Beanstalk you can insert as many jobs as required. You can define the message structure according to the needs of the application:
~~~
<?php
use Phalcon\Queue\Beanstalk;
// Connect to the queue
$queue = new Beanstalk(
[
"host" => "192.168.0.21",
"port" => "11300",
]
);
// Insert the job in the queue
$queue->put(
[
"processVideo" => 4871,
]
);
~~~
Available connection options are:
| Option | Description | Default |
| --- | --- | --- |
| host | IP where the beanstalk server is located | 127.0.0.1 |
| port | Connection port | 11300 |
In the above example we stored a message which will allow a background job to process a video. The message is stored in the queue immediately and does not have a certain time to live.
Additional options as time to run, priority and delay can be passed as second parameter:
~~~
<?php
// Insert the job in the queue with options
$queue->put(
[
"processVideo" => 4871,
],
[
"priority" => 250,
"delay" => 10,
"ttr" => 3600,
]
);
~~~
The following options are available:
| Option | Description |
| --- | --- |
| priority | It’s an integer < 2\*\*32. Jobs with smaller priority values will be scheduled before jobs with larger priorities. The most urgent priority is 0; the least urgent priority is 4,294,967,295. |
| delay | It’s an integer number of seconds to wait before putting the job in the ready queue. The job will be in the “delayed” state during this time. |
| ttr | Time to run – is an integer number of seconds to allow a worker to run this job. This time is counted from the moment a worker reserves this job. |
Every job put into the queue returns a “job id” which you can use to track the status of the job:
~~~
<?php
$jobId = $queue->put(
[
"processVideo" => 4871,
]
);
~~~
## 檢索信息(Retrieving Messages)
Once a job is placed into the queue, those messages can be consumed by a background worker which will have enough time to complete the task:
~~~
<?php
while (($job = $queue->peekReady()) !== false) {
$message = $job->getBody();
var_dump($message);
$job->delete();
}
~~~
Jobs must be removed from the queue to avoid double processing. If multiple background jobs workers are implemented, jobs must be “reserved” so other workers don’t re-process them while other workers have them reserved:
~~~
<?php
while (($job = $queue->reserve()) !== false) {
$message = $job->getBody();
var_dump($message);
$job->delete();
}
~~~
Our client implement a basic set of the features provided by Beanstalkd but enough to allow you to build applications implementing queues.
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl