将多个变量传递给 Gearman 工作函数
如何将两个变量传递给同一个工作函数?例如,假设我希望连接从客户端传递的两个字符串。我在一些示例代码中看到正在使用一个数组,但我无法让它工作。
<?php
$client= new GearmanClient();
$client->addServer();
$arguments = array(
"string1" => "hey",
"string2" => "there"
);
$client->addTask("string_concat", $arguments);
$client->runTasks();
?>
然而,这告诉我这是一个无效的工作负载(我认为因为它是一个正在传递的数组)。我应该如何传递它们——我应该为每个任务创建一个任务吗?
那么如果我无法发送数组,如何在工作函数中使用多个变量。我尝试过类似函数 String_Concat($job, $job2) 但后来我不确定如何将它们添加到工作负载()
如果我能够传递数组,这里是一些示例代码:
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("string_concat", "String_Concat");
while ($worker->work());
function String_Concat($job)
{
$arguments = $job->workload();
return $arguments["string1"] . $arguments["string2"];
}
?>
什么是最好的方法做这个吗?多谢!
How do you pass two variables to the same worker function? For example, say I wished to concat two strings that I pass from the client. I saw in some example code an array being used, but I can't get it to work.
<?php
$client= new GearmanClient();
$client->addServer();
$arguments = array(
"string1" => "hey",
"string2" => "there"
);
$client->addTask("string_concat", $arguments);
$client->runTasks();
?>
This tells me it's an invalid workload however (I assume cause it's an array being passed). How should I be passing them - should I create a task for each?
Then if I can't send an array, how can I use multiple variables in the worker function. I've tried like function String_Concat($job, $job2) but then I'm not sure how I'd add them to the workload()
Here is some example code if I were able to pass arrays:
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("string_concat", "String_Concat");
while ($worker->work());
function String_Concat($job)
{
$arguments = $job->workload();
return $arguments["string1"] . $arguments["string2"];
}
?>
What's the best way to do this? Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该将其序列化。
比如:
然后,从你的员工那里,你可以做类似的事情......
You should serialize it.
Something like:
Then, from your worker, you could do something like...