【发布时间】:2016-05-09 13:33:14
【问题描述】:
我对如何在我的 php 应用程序中有效地使用 gearman 有一些疑问。
我正在使用 inotify 监控一个文件夹,其中将存储和处理大量文件(每次超过 1000 个)。 用于解析它们中的每一个并将其内容保存在数据库中,我正在尝试使用 gearman。
while(true){
sleep(5); # spare some CPU Cycles
set_time_limit(0); # unlimited timeout request
// read events
$events = inotify_read($this->instance);
// if the event is happening within our 'Files directory'
if ($events[0]['wd'] === $this->watch_id){
foreach ($events as $key=>$value)
{
if($events[$key]['mask'] === IN_CREATE){
# A new file was created in folder
$client = new \GearmanClient();
$client->addServer();
$client->addTask("parse_file", $events[$key]['name']); # add task to parse that file
printf("Created file: %s in Files directory\n", $events[$key]['name']);
}
else if ($events[$key]['mask'] === IN_DELETE){
printf("Deleted file: %s in Files directory\n", $events[$key]['name']);
}
}
if(!is_null($client)){ # once everything is done, run the tasks.
$client->runTasks();
}
}
}
我创建了一个这样的worker.php文件:
<?php
namespace controllers;
use app\file\File;
require_once 'vendor/autoload.php';
$worker = new \GearmanWorker();
$worker->addServer();
$worker->addFunction('parse_file', function($job){
echo "entrou no add function!<br>";
print_r ($job->workload());
sleep(2);
return new File($job->workload()); # this class parses the files content in database
});
while ($worker->work());
事情正在发生。 worker 函数运行,第一个文件的数据存入数据库但出现错误:
这是我的 nohup.out 文件的输出。
Catchable fatal error: Object of class app\file\File could not be converted to string in /var/www/html/worker.php on line 18
“他”想要什么? :)
【问题讨论】:
标签: php gearman inotify php-5.5