将多个变量传递给 Gearman 工作函数

发布于 2024-12-22 07:53:34 字数 864 浏览 2 评论 0原文

如何将两个变量传递给同一个工作函数?例如,假设我希望连接从客户端传递的两个字符串。我在一些示例代码中看到正在使用一个数组,但我无法让它工作。

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2024-12-29 07:53:34

你应该将其序列化。

比如:

$data = serialize( $array );
$client->addTask("string_concat", $data);

然后,从你的员工那里,你可以做类似的事情......

if (is_string($data) && $data = unserialize($workload)) {

} else {
 // Maybe throw Exception or something?
}

You should serialize it.

Something like:

$data = serialize( $array );
$client->addTask("string_concat", $data);

Then, from your worker, you could do something like...

if (is_string($data) && $data = unserialize($workload)) {

} else {
 // Maybe throw Exception or something?
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文