Executing functions in parallel

前端 未结 6 1654
情歌与酒
情歌与酒 2020-12-16 18:55

I have a function that needs to go over around 20K rows from an array, and apply an external script to each. This is a slow process, as PHP is waiting for the script to be e

6条回答
  •  [愿得一人]
    2020-12-16 19:38

    What you are looking for is parallel which is a succinct concurrency API for PHP 7.2+

    $runtime = new \parallel\Runtime();
    
    $future = $runtime->run(function() {
        for ($i = 0; $i < 500; $i++) {
            echo "*";
        }
    
        return "easy";
    });
    
    for ($i = 0; $i < 500; $i++) {
        echo ".";
    }
    
    printf("\nUsing \\parallel\\Runtime is %s\n", $future->value());
    

    Output:

    .*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
    Using \parallel\Runtime is easy
    

提交回复
热议问题