Laravel命令,Pthreads和关闭

问题描述 投票:1回答:2

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

例如,Laravel之外的一个简单脚本工作正常,我喜欢结果。我决定搬进Laravel,面对这个问题。当然我在谷歌搜索,在stackoverflow上发现了一些问题,其中回复了扩展的作者。但我没有帮助他的答案,所以我请你帮助我。

Answered问题扩展的作者。

有一个类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

谢谢你们!

pthreads laravel-5.2 php-closures php-pthread
2个回答
1
投票
foreach ($this->pool as $w) {
   $w->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);
}

1
投票

首先,您可以了解图书馆作者的策略: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和pthreads:

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
© www.soinside.com 2019 - 2024. All rights reserved.