PHP 7.2.0 Beta 3 Released


(PECL gearman >= 0.5.0)

GearmanClient::doBackgroundRun a task in the background


public string GearmanClient::doBackground ( string $function_name , string $workload [, string $unique ] )

Runs a task in the background, returning a job handle which can be used to get the status of the running task.



O funcție înregistrată pe care trebuie să o execute serverul executor


Datele serializate pentru a fi procesate


Un ID unical utilizat pentru a identifica o anumită sarcină

Valorile întoarse

The job handle for the submitted task.


Example #1 Submit and monitor a background job

The worker in this example has an artificial delay introduced to mimic a long running job. The client script periodically checks the status of the running job.


/* create our object */
$gmclient= new GearmanClient();

/* add the default server */

/* run reverse client */
$job_handle $gmclient->doBackground("reverse""this is a test");

if (
$gmclient->returnCode() != GEARMAN_SUCCESS)
"bad return code\n";

$done false;
$stat $gmclient->jobStatus($job_handle);
   if (!
$stat[0]) // the job is known so it is not done
$done true;
"Running: " . ($stat[1] ? "true" "false") . ", numerator: " $stat[2] . ", denomintor: " $stat[3] . "\n";



Exemplul de mai sus va afișa ceva similar cu:

Running: true, numerator: 3, denomintor: 14
Running: true, numerator: 6, denomintor: 14
Running: true, numerator: 9, denomintor: 14
Running: true, numerator: 12, denomintor: 14
Running: false, numerator: 0, denomintor: 0

A se vedea și

add a note add a note

User Contributed Notes 1 note

felix at passcod dot name
1 day ago
Behaviour of the unique ID argument:

If it's not provided, it defaults to a UUIDv1 (timestamp + mac address).

Otherwise, if a job with the same unique ID exists (i.e. is either queued or currently executing), then that job is used, not the one you're submitting. This does not apply to past (completed) jobs, and works across the entire job server pool, assuming no partitioning.

Or with code (the values in [brackets] are the server-assigned job identifiers):


->doBackground('sleep', '3', '123'); // [H:host:1] Starts sleep(3)
$gearman->doBackground('sleep', '5', '456'); // [H:host:2] Queues sleep(5)
$gearman->doBackground('sleep', '3', '123'); // [H:host:1] Does nothing
$gearman->doBackground('sleep', '1', '123'); // [H:host:1] Also does nothing (diff args don't trigger new jobs)

sleep (3);
// Job 123 [sleep(3)] has finished by now

$gearman->doBackground('sleep', '3', '123'); // [H:host:3] Starts a new job

To Top