I
I
Igor Tkachenko2015-02-10 09:07:42
PHP
Igor Tkachenko, 2015-02-10 09:07:42

How to run many tasks at once on gearman in parallel in php?

Actually, I use the construction of adding tasks like this:

$jobs = DB::select()
  ->from('jobs')					
  ->where('status', '=', 'wait')													
  ->and_where('active', '=', 1)		
  ->execute()
  ->as_array();	

foreach($jobs as $job)
{						
  $update = DB::update('jobs')
    ->set(array('status' => 'process'))
    ->where('id', '=', $job['id'])
    ->execute();
  
  $client = new GearmanClient(); 
  # Подключаемся к серверу Gearman 				
  $client->addServer(); 				
  # Шлем сообщение 
  $client->doBackground('Task', json_encode($job));			

  sleep(1);																
}

All this works through the supervisor, then I execute the added tasks (another file):
$worker->addServer(); 
$worker->addFunction('TaskSpam', array($this, "Task")); 

while (1) {
    $ret = $worker->work(); 
}

But the trouble is that with such a design, it performs tasks in turn, which does not interest me at all when solving this problem, it is precisely the parallel execution of 10.20 ... and more tasks, for example.
There are examples?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2015-02-10
@fornit1917

Launch multiple workers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question