protected function handleTasks() { $this->client->runTasks(); $this->logger->debug("Client finished."); $this->logger->debug("Start tasks inspection."); $this->inspectionDto = $this->tasksInspector->inspect(); $this->logger->debug($this->inspectionDto->getInspectionMessage()); $this->logger->notice("Inspection message: " . serialize($this->inspectionDto->getInspectionMessage())); $this->handleInspection(); return null; }
/** * Divides the passed workload and handles the division of workload between * multiple instances */ public function before() { // If original request and above the workable limit then we // look to parallelise the work $m_target = $this->request->post($this->_key_name); if ( $this->request->is_initial() AND isset($m_target) AND is_array($m_target) AND count($m_target) >= $this->_workable_limit ) { // Instantiate gearman client $obj_gearman = new GearmanClient; $obj_gearman->addServer(); // Divide the work into $this->_worker_count chunks for processing $int_chunk_size = round( count($m_target) / $this->_worker_count ); $arr_chunks = array_chunk( $m_target, $int_chunk_size ); // Reverse the route.. $str_route = $this->request->uri(); // Update the controller action to our own nullifier $this->request->action('nullifier'); // Schedule each of the requests $c = 0; foreach ($arr_chunks as $chunk) { // Format the string to be passed to the worker by formatting the post $arr_d = $_POST; $arr_d[$this->_key_name] = $arr_chunks[$c]; $str_data = $str_route . "#" . http_build_query($arr_d); $obj_gearman->addTask('make_request', $str_data); $c++; } // Set the complete requests callback $obj_gearman->setCompleteCallback(array($this,"complete")); // Execute the requests $obj_gearman->runTasks(); } }
/** * @group concurrency */ public function testConcurrency() { if (!class_exists('GearmanClient', false)) { $this->markTestSkipped('pecl/gearman is required for this test to run.'); } $client = new \GearmanClient(); $client->addServer(); $workerIds = []; $poppedItems = []; $client->setCompleteCallback(function (\GearmanTask $task) use(&$workerIds, &$poppedItems) { $data = explode(':', $task->data(), 2); if (!is_array($data) || 2 !== count($data)) { return; } list($workerId, $item) = $data; $workerIds[$workerId] = true; if (!isset($poppedItems[$item])) { $poppedItems[$item] = true; } }); $queueSize = $this->getConcurrencyQueueSize(); $this->assertGreaterThan(10, $queueSize, 'Queue size is too small to test concurrency.'); $workload = serialize(self::getHandler()); for ($i = 1; $i <= $queueSize; $i++) { $this->queue->push($i); $client->addTask('pop', $workload); } try { // run the tasks in parallel (assuming multiple workers) $result = $client->runTasks(); } catch (\GearmanException $e) { $result = false; } if (!$result) { $this->markTestSkipped('Unable to run gearman tasks. Check if gearman server is running.'); } $this->assertEquals($queueSize, count($poppedItems)); $this->assertGreaterThan(1, count($workerIds), 'Not enough workers to test concurrency.'); }
$data[$x]['src'] = $_SERVER['argv'][1]; $data[$x]['dest'] = "{$x}.jpg"; $data[$x]['x'] = (80 + 1) * ($x + 1); $data[$x]['y'] = NULL; } /* fire off each job */ foreach ($data as $img) { /* NOTE: if you want to asynchronously queue jobs use ** $task= $gmc->add_task_background("shrink_image", serialize($img)); ** however keep in mind that your complete callback will not get called */ if (!$gmc->addTask("shrink_image", serialize($img))) { echo "ERROR RET: " . $gmc->error() . "\n"; exit; } } if (!$gmc->runTasks()) { echo "ERROR RET:" . $gmc->error() . "\n"; exit; } echo "DONE\n"; exit; function thumb_created($task) { echo "CREATED -> job: " . $task->jobHandle() . "\n"; } function thumb_complete($task) { echo "COMPLETE -> job: " . $task->jobHandle() . " new_file: " . $task->data() . "\n"; } function thumb_fail($task) {
/** * Метод, который содержит основную бизнес-логику клиента. По умолчанию просто запускает все задачи. * Если возвращает false, то работа клиента прерывается (!) * @return boolean */ public function process() { parent::runTasks(); return true; }
public function run() { $this->init(); $this->addTasks(); $this->gc->runTasks(); }
public function runTasks() { return $this->gearmanClient->runTasks(); }
$tt1 = microtime(true); $ctg = new data(array('server' => '10.102.1.3', 'user' => 'sa', 'pass' => 'i3kygbb2', 'database' => 'sifinca', 'engine' => 'mssql')); $inmueble = new inmuebles($ctg); echo " Cargando inmuebles ->"; $inmuebles = $inmueble->getInmuebles(array('promocion' => 0)); echo count($inmuebles) . "\n"; $client = new GearmanClient(); //por defecto el localhost $client->addServer(); $client->setCompleteCallback("complete"); $map = array("total" => 0, "data" => null); $json = json_encode($map); // crear task $i = 0; foreach ($inmuebles as $row) { $i++; $json = json_encode(array("id" => 'C' . $row['id_inmueble'])); $job_handle = $client->addTask("delete", $json, null, $i); echo "Enviando Tasks -> {$i} \n"; } if (!$client->runTasks()) { echo "ERROR " . $client->error() . "\n"; exit; } $tt2 = microtime(true); $r = $tt2 - $tt1; echo "\n\nTiempo de " . $r . " para {$i} registros\n"; function complete($task) { print "COMPLETE: " . $task->unique() . "\n"; }
<?php $client = new GearmanClient(); $client->addServer('127.0.0.1', 4730); //本机可以直接addServer(),默认服务器端使用4730端口 $client->setCompleteCallback('completeCallBack'); //先绑定才有效 $result1 = $client->do('say', 'do'); //do是同步进行,进行处理并返回处理结果。 $result2 = $client->doBackground('say', 'doBackground'); //异步进行,只返回处理句柄。 $result3 = $client->addTask('say', 'addTask'); //添加任务到队列,同步进行?通过添加task可以设置回调函数。 $result4 = $client->addTaskBackground('say', 'addTaskBackground'); //添加后台任务到队列,异步进行? $client->runTasks(); //运行队列中的任务,只是do系列不需要runTask()。 echo 'result1:'; var_dump($result1); echo '<br/>'; echo 'result2:'; var_dump($result2); echo '<br/>'; echo 'result3:'; var_dump($result3); echo '<br/>'; echo 'result4:'; var_dump($result4); echo '<br/>'; //绑定回调函数,只对addTask有效 function completeCallBack($task)
/** * Executes an asynchronous request using the driver * method. * * // Execute the asynchronous request * $driver->execute($request_async); * * @param Request_Async The asynchronous request to execute * @return Request_Async */ public function execute(Request_Async $request_async) { // Assign the asynchronous request to this driver $this->_request_async = $request_async; // Foreach request foreach ($request_async as $request) { // Add the task to the job $task = $this->_gearman_client->addTaskHigh('request_async', serialize($request), Request_Async_Gearman::$context); $uuid = $task->unique(); $this->_requests[$uuid] = $request; $this->_task_handles[] = $task; $this->_complete[$uuid] = NULL; } // Run the tasks $this->_gearman_client->runTasks(); // Return request async object return $request_async; }
private function _processUpload($uploader, $cid = 0) { $photoModel = new Models\Photo(); if ($photoModel->create($uploader, array('cid' => $cid))) { $result['id'] = $photoModel->get_pid(); $result['md5'] = $photoModel->md5; $result['width'] = $photoModel->width; $result['height'] = $photoModel->height; $result['mime'] = $photoModel->mime; $result['is_animated'] = $photoModel->is_animated; $imgurls = $photoModel->geturi($result['id'], 130); $result['src'] = $imgurls[0]; $thumb = new Thumb(); $thumb->resize(NULL, $result['id'], 130); $client = new GearmanClient(); $client->addServers(Core::config('job_servers')); $client->addTaskLowBackground("thumbnail", serialize(array($result['id'], 320))); $client->addTaskLowBackground("thumbnail", serialize(array($result['id'], 780))); $client->addTaskLowBackground("thumbnail", serialize(array($result['id'], 1600))); @$client->runTasks(); return $result; } else { return FALSE; } }