PHP 8.4.1 Released!

GearmanClient::doNormal

(No version information available, might only be in Git)

GearmanClient::doNormalRun a single task and return a result

Descrição

public GearmanClient::doNormal(string $function, string $workload, ?string $unique = null): string

Runs a single task and returns a string representation of the result. It is up to the GearmanClient and GearmanWorker to agree on the format of the result.

Parâmetros

function

Uma função registrada que o trabalhador deve executar

workload

Dados serializados a serem processados

unique

Um identificador único usado para identificar uma tarefa específica

Valor Retornado

A string representing the results of running a task.

Exemplos

Exemplo #1 Simple job submission with immediate return

<?php

?>
<?php

# Client code

echo "Starting\n";

# Create our client object.
$gmclient= new GearmanClient();

# Add default server (localhost).
$gmclient->addServer();

echo
"Sending job\n";

$result = $gmclient->doNormal("reverse", "Hello!");

echo
"Success: $result\n";

?>
<?php

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");

print
"Waiting for job...\n";
while(
$gmworker->work())
{
if (
$gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo
"return_code: " . $gmworker->returnCode() . "\n";
break;
}
}

function
reverse_fn($job)
{
return
strrev($job->workload());
}

?>

O exemplo acima produzirá algo semelhante a:

Starting
Sending job
Success: !olleH

Exemplo #2 Submitting a job and retrieving incremental status

A job is submitted and the script loops to retrieve status information. The worker has an artificial delay which results in a long running job and sends status and data as processing occurs. Each subsequent call to GearmanClient::doNormal() produces status information on the running job.

<?php

# Client code

# Create our client object.
$gmclient= new GearmanClient();

# Add default server (localhost).
$gmclient->addServer();

echo
"Sending job\n";

# Send reverse job
do
{
$result = $gmclient->doNormal("reverse", "Hello!");
# Check for various return packets and errors.

switch($gmclient->returnCode())
{
case
GEARMAN_WORK_DATA:
echo
"Data: $result\n";
break;
case
GEARMAN_WORK_STATUS:
list(
$numerator, $denominator)= $gmclient->doStatus();
echo
"Status: $numerator/$denominator complete\n";
break;
case
GEARMAN_WORK_FAIL:
echo
"Failed\n";
exit;
case
GEARMAN_SUCCESS:
break;
default:
echo
"RET: " . $gmclient->returnCode() . "\n";
echo
"Error: " . $gmclient->error() . "\n";
echo
"Errno: " . $gmclient->getErrno() . "\n";
exit;
}
}
while(
$gmclient->returnCode() != GEARMAN_SUCCESS);

echo
"Success: $result\n";

?>
<?php

# Worker code

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");

print
"Waiting for job...\n";
while(
$gmworker->work())
{
if (
$gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo
"return_code: " . $gmworker->returnCode() . "\n";
break;
}
}

function
reverse_fn($job)
{
echo
"Received job: " . $job->handle() . "\n";

$workload = $job->workload();
$workload_size = $job->workloadSize();

echo
"Workload: $workload ($workload_size)\n";

# This status loop is not needed, just showing how it works
for ($x= 0; $x < $workload_size; $x++)
{
echo
"Sending status: " + $x + 1 . "/$workload_size complete\n";
$job->sendStatus($x+1, $workload_size);
$job->sendData(substr($workload, $x, 1));
sleep(1);
}

$result= strrev($workload);
echo
"Result: $result\n";

# Return what we want to send back to the client.
return $result;
}

?>

O exemplo acima produzirá algo semelhante a:

Worker output:

Starting
Waiting for job...
Received job: H:foo.local:106
Workload: Hello! (6)
1/6 complete
2/6 complete
3/6 complete
4/6 complete
5/6 complete
6/6 complete
Result: !olleH

Client output:

Starting
Sending job
Status: 1/6 complete
Data: H
Status: 2/6 complete
Data: e
Status: 3/6 complete
Data: l
Status: 4/6 complete
Data: l
Status: 5/6 complete
Data: o
Status: 6/6 complete
Data: !
Success: !olleH

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 2 notes

up
0
stanislav dot reshetnev at gmail dot com
10 years ago
doNormal don't work in version php_gearman 0.8.3:

PHP Fatal error: Call to undefined method GearmanClient::doNormal()

But it exist in version 1.1.2.
up
-1
fastest963 at gmail dot com
11 years ago
do() or doNormal() will block until the job is accepted by the worker. Looping is only necessary if you care about the response from the job. Use setTimeout() to control how long PHP will block.

If it does timeout, it throws a PHP Warning:
PHP Warning: GearmanClient::do(): _client_do(GEARMAN_TIMEOUT) occured during gearman_client_run_tasks() -> libgearman/client.cc:174
To Top