首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

GearmanWorker::addFunction

(PECL gearman >= 0.5.0)

GearmanWorker::addFunction - 注册并添加回调函数

描述

代码语言:javascript
复制
public bool GearmanWorker::addFunction ( string $function_name , callable $function [, mixed &$context [, int $timeout ]] )

向作业服务器注册一个函数名称,并指定与该函数相对应的回调函数。可以指定在调用回调时使用的额外应用程序上下文数据以及超时。

参数

function_name

向作业服务器注册的函数的名称

function

提交注册函数名称作业时调用的回调函数

context

对可由worker函数修改的任意应用程序上下文数据的引用

timeout

以秒为单位的时间间隔

返回值

成功时返回TRUE或失败时返回FALSE

例子

示例#1简单的工作人员利用额外的应用程序上下文数据

代码语言:javascript
复制
<?php

# get a gearman worker
$worker= new GearmanWorker(); 

# add the default server (localhost)
$worker->addServer(); 

# define a variable to hold application data
$count= 0; 

# add the "reverse" function
$worker->addFunction("reverse", "reverse_cb", $count);

# start the worker
while ($worker->work());

function reverse_cb($job, &$count) 
{ 
  $count++; 
  return "$count: " . strrev($job->workload()); 
} 

?>

运行为反向函数提交两个作业的客户端将具有类似于以下内容的输出:

代码语言:javascript
复制
1: olleh
2: dlrow

另请参阅

← GearmanWorker

GearmanWorker::addOptions →

扫码关注腾讯云开发者

领取腾讯云代金券