利用Gearman實現并發查詢(Multi-Query)

fdabobi 8年前發布 | 10K 次閱讀 PHP開發

來自: http://blog.csdn.net//jiao_fuyou/article/details/27170743


這個例子是想從數據庫查詢出幾個結果集,一般的做法是,一個接一個的發送查詢,然后匯總結果進行輸出。

下面我們利用Gearman的gearman_client_run_tasks實現并發的查詢,gearman_client_run_tasks接口可以一次性的提交多個任務,然后在callback函數中異步的處理結果。


PHP的示例如下:

The client:

<?php

$client = new GearmanClient();
$client->addServer();

// initialize the results of our 3 "query results" here
$userInfo = $friends = $posts = null;

// This sets up what gearman will callback to as tasks are returned to us.
// The $context helps us know which function is being returned so we can 
// handle it correctly.
$client->setCompleteCallback(function(GearmanTask $task, $context) use (&$userInfo, &$friends, &$posts) {
    switch($context) {
        case 'lookup_user':
            $userInfo = $task->data();
            break;
        case 'baconate':
            $friends = $task->data();
            break;
        case 'get_latest_posts_by':
            $posts = $task->data();
            break;
    }
});

// Here we queue up multiple tasks to be execute in *as much* parallelism as gearmand can give us
$client->addTask('lookup_user', 'joe@joe.com', 'lookup_user');
$client->addTask('baconate', 'joe@joe.com', 'baconate');
$client->addTask('get_latest_posts_by', 'joe@joe.com', 'get_latest_posts_by');

echo "Fetching...\n";
$start = microtime(true);
$client->runTasks();
$totaltime = number_format(microtime(true) - $start, 2);

echo "Got user info in: $totaltime seconds:\n";
var_dump($userInfo, $friends, $posts);

The worker:

<?php

$worker = new GearmanWorker();
$worker->addServer();

$worker->addFunction('lookup_user', function(GearmanJob $job){
    // normally you'd so some very safe type checking and query binding to a database here.
    // ...and we're gonna fake that.
    sleep(3);
    return 'The user requested ('. $job->workload() .') is 7 feet tall and awesome';
});

$worker->addFunction('baconate', function(GearmanJob $job){
    sleep(3);
    return 'The user ('. $job->workload() .') is 1 degree away from Kevin Bacon';
});

$worker->addFunction('get_latest_posts_by', function(GearmanJob $job){
    sleep(3);
    return 'The user ('. $job->workload() .') has no posts, sorry!';
});

while ($worker->work());

運行結果:

Fetching...
Got user info in: 9.00 seconds:
string(59) "The user requested (joe@joe.com) is 7 feet tall and awesome"
string(56) "The user (joe@joe.com) is 1 degree away from Kevin Bacon"
string(43) "The user (joe@joe.com) has no posts, sorry!"</span>

發現還是用了9S時間,那是因為只有一個worker,它只能是順序執行。


一起運行三個worker或更多,再來試試:

Fetching...
Got user info in: 3.00 seconds:
string(59) "The user requested (joe@joe.com) is 7 feet tall and awesome"
string(56) "The user (joe@joe.com) is 1 degree away from Kevin Bacon"
string(43) "The user (joe@joe.com) has no posts, sorry!"</span>


這只是一個并行執行的例子,實際應用中可以有更好的并發性能。




 本文由用戶 fdabobi 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!