1

一个特定的进程需要执行多个线程。我了解了 php 的扩展 - pthreads。

例如,在 Laravel 之外的一个简单脚本运行良好,我喜欢它的结果。我决定搬到 Laravel,并面临这个问题。当然我在google上搜索,在stackoverflow上发现了一些问题,其中回复了扩展的作者。但我没有帮他回答,所以我请你帮帮我。

已回答问题扩展作者。

有一个类 App\Commands\QuestionsParserCommand。在里面,我创建了 App\My\Questions\QuestionsParser 类的一个实例,并调用了方法 init()。然后是方法init()的代码:

// Create a pool
$pool = new Pool($this->threads, ParserWorkers::class);

// Create a thread class
$thread = new class extends Threaded
{
    public function run()
    {
      // The class will receive data from a provider
      // that will be shared between threads through ParserWorkers.
      // It will work with the API and store the data in the database.
      // The need to work with the threads,
      // because the data for processing an incredible amount.

      echo '+';
    }
};

// Start a threads
for ($i = 0; $i < $this->threads; $i++) {
    $pool->submit($thread);
}

$pool->shutdown();

ParserWorkers 类继承自 Worker,但有一个空方法 run()。

结果,我运行脚本并在 php 的日志中得到一条消息:

[13-Oct-2016 11:27:35 Europe/Moscow] PHP Fatal error:  Uncaught Exception: Serialization of 'Closure' is not allowed in [no active file]:0
Stack trace:
#0 {main}
    thrown in [no active file] on line 0

信息:Laravel 5.2.43、php 7.0.8、Windows

谢谢大家!

4

2 回答 2

2

首先可以了解一下库作者的策略:https ://github.com/krakjoe/pthreads-autoloading-composer

这里使用的策略确保每个线程 (Worker) 获得框架的线程本地副本,或者正在加载的任何内容,但不会破坏从 pthreads 下降的对象的能力。


其次,要开始使用 laravel 功能(如服务提供者、门面、助手等),您需要初始化 laravel。

我查看了如何在 tests/CreatesApplication.php 文件中初始化应用程序。下面的代码展示了如何使用 php 7.2 为 Laravel 5.7 执行此操作。

重要提示:指定 autoload.php 和 app.php 相对于 Autoloader.php 文件位置的正确路径。

namespace App\Console\Commands;

use Worker;

use Illuminate\Contracts\Console\Kernel;

class Autoloader extends Worker
{
    public function run()
    {
        require __DIR__. '/../../../vendor/autoload.php';
        $app = require __DIR__.'/../../../bootstrap/app.php';
        $app->make(Kernel::class)->bootstrap();
    }

    public function start($options = PTHREADS_INHERIT_ALL)
    {
        return parent::start(PTHREADS_INHERIT_INI);
    }

}
namespace App\Console\Commands;

use Exception;
use Threaded;

class OperatorThread extends Threaded
{
    /**
     * @var OperatorThreaded
     */
    private $operator;
    private $error;

    public function __construct(OperatorThreaded $operator)
    {
        $this->operator = $operator;
    }

    public function run()
    {
        try {
            $this->operator->handle();
        } catch (Exception $exception) {
            $this->error = (string) $exception;
        }
    }

    public function getError() {
        return $this->error;
    }

}
namespace App\Console\Commands;

class OperatorThreaded
{
    private $i;

    public function __construct($i)
    {
        $this->i = $i;
    }

    public function handle()
    {
        sleep(rand(1,3));
        if (app()->isBooted()) {
            echo $this->i . PHP_EOL;
        }
    }

}

现在您可以将 Laravel 与 pthread 一起使用:

namespace App\Console\Commands;

use Pool;

use Illuminate\Console\Command;

class Test extends Command
{
    protected $description = 'test';
    protected $signature = 'test';

    public function handle()
    {
        $operators = [];
        for ($i=0; $i<5; $i++) {
            $operators[] = new OperatorThreaded($i);
        }

        $pool = new Pool(count($operators), Autoloader::class);
        foreach ($operators as $operator) {
            $thread = new OperatorThread($operator);
            $pool->submit($thread);
        }

        while ($pool->collect());
        $pool->shutdown();
    }
}
$ php artisan test
1
2
4
0
3
于 2019-03-14T10:21:08.780 回答
1
foreach ($this->pool as $w) {
   $w->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);
}
于 2017-01-03T07:23:42.833 回答