简体   繁体   中英

How to run a PHP function concurrently against an array?

I have a PHP web app which scrapes a search engine for a given keyword.

Currently the app is looping through an array of keywords running the scrape function one keyword at a time.

This is OK at the moment because the number of keywords is fairly small, but it won't scale well.

I think the best way to go will be to select a smaller set of keywords from the mysql db using limit, and then run the scrape function concurrently against the entire array. Once that set has finished, I'll move on to the next set.

But I'm stuck with how to run the function concurrently against the array.

How would you handle this?

PHP本身没有任何并发​​性,但如果使用cURL获得搜索结果,则cURL扩展中会有多个请求功能 ,因此您可以并行化至少获取结果。

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
    echo "$key. $item2<br />\n";
}

echo "Before ...:\n";
array_walk($fruits, 'test_print');

array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";

array_walk($fruits, 'test_print');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM