【发布时间】:2017-02-25 20:02:16
【问题描述】:
我尝试使用 gearman 在 PHP 中运行任务,我创建了 2 个脚本: 客户端.php
<?
$mail = array(
'to' => 'test@gmail.com',
'subject' => Hi',
'body' => 'Test message',
);
# Connect to Gearman server
$client = new GearmanClient();
$client->addServer('127.0.0.1', '4730');
# Send message
$client->doBackground('sendmail', json_encode($mail));
worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('sendmail', 'send_mail');
while (1)
{
$worker->work();
if ($worker->returnCode() != GEARMAN_SUCCESS) break;
}
function send_mail($job)
{
$workload = $job->workload();
$data = json_decode($workload, true);
mail($data['to'], $data['subject'], $data['body']);
}
当我从命令行运行我的工作人员时:php worker.php &
并运行我的 client.php 文件,我得到以下错误:
GearmanClient::doBackground(): send_packet(GEARMAN_COULD_NOT_CONNECT) 发送服务器选项包失败 -> libgearman/connection.cc:485
有什么帮助吗?
谢谢
【问题讨论】:
-
我也会在工作端明确定义服务器。
$worker->addServer('127.0.0.1', '4730');. -
你的 gearman 服务器在运行吗?检查 'ps awux |grep gearmand'
-
我在运行你的命令时得到这个:root 4515 0.0 0.0 103308 880 pts/0 S+ 05:49 0:00 grep gearmand